From 59edb5c49c3b8add5daef5c7d58c970c780d4737 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Sat, 23 Sep 2023 17:42:24 -0400 Subject: [PATCH 1/2] Document LiteralReal --- docs/src/manual/types.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/src/manual/types.md b/docs/src/manual/types.md index 0b2fcffd8..ce5bb050a 100644 --- a/docs/src/manual/types.md +++ b/docs/src/manual/types.md @@ -42,3 +42,38 @@ typeof(Z) typeof(s) ``` +## Canonicalization, and avoiding it + +By default, Symbolics will simplify expressions to an internal canonical form upon construction: + +```@example types +@variables x y +(x + y, y + x) # assumes commutativity of + +``` +```@examples types +x + y + y + x # assumes associativity also +``` +```@examples types +x^2 * y * x # same goes for * +``` +```@examples types +x/(x*y) # assumes x!=0 +``` + +These assumptions are made so that the obvious simplifications are done as soon as someone writes them. If you want to ask Symbolics to not make these assumptions, you can annotate them with the `::LiteralReal` type when declaring them with `@variables`. By default, variables are implicitly annotated by `::Real`, `::LiteralReal` is an internal type which is a subtype of `Real` which is used to create expressions with the property that they are not auto-simplified. + +```@examples types +@variables a::LiteralReal b::LiteralReal + +(a + b, + b + a, + a + b + b + a, + a^2 * b * a, + a/(a*b)) + +You can later force expressions to be simplified by using the `simplify` function. + + +```@examples types +simplify(a + b + b + a) +``` From fb67a35255a67a8add7a508c33928e65e57f6074 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Sat, 23 Sep 2023 19:31:30 -0400 Subject: [PATCH 2/2] reftest updates --- test/build_function_tests/stencil-extents-inplace.jl | 2 +- test/build_function_tests/stencil-extents-outplace.jl | 2 +- test/solver.jl | 2 +- test/target_functions/issue123.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/build_function_tests/stencil-extents-inplace.jl b/test/build_function_tests/stencil-extents-inplace.jl index e8f52fc21..20069daef 100644 --- a/test/build_function_tests/stencil-extents-inplace.jl +++ b/test/build_function_tests/stencil-extents-inplace.jl @@ -6,7 +6,7 @@ ˍ₋out_2 = (view)(ˍ₋out, 2:4, 2:4) for (j, j′) = zip(2:4, reset_to_one(2:4)) for (i, i′) = zip(2:4, reset_to_one(2:4)) - ˍ₋out_2[i′, j′] = (+)(ˍ₋out_2[i′, j′], (*)(1//2, (+)((+)((+)((getindex)(x, i, (+)(1, j)), (getindex)(x, i, (+)(-1, j))), (getindex)(x, (+)(-1, i), j)), (getindex)(x, (+)(1, i), j)))) + ˍ₋out_2[i′, j′] = (+)(ˍ₋out_2[i′, j′], (*)(1//2, (+)((+)((+)((getindex)(x, (+)(-1, i), j), (getindex)(x, (+)(1, i), j)), (getindex)(x, i, (+)(-1, j))), (getindex)(x, i, (+)(1, j))))) end end end diff --git a/test/build_function_tests/stencil-extents-outplace.jl b/test/build_function_tests/stencil-extents-outplace.jl index e7f988a36..e063cf0b3 100644 --- a/test/build_function_tests/stencil-extents-outplace.jl +++ b/test/build_function_tests/stencil-extents-outplace.jl @@ -5,7 +5,7 @@ ˍ₋out_2 = (view)(ˍ₋out, 2:4, 2:4) for (j, j′) = zip(2:4, reset_to_one(2:4)) for (i, i′) = zip(2:4, reset_to_one(2:4)) - ˍ₋out_2[i′, j′] = (+)(ˍ₋out_2[i′, j′], (*)(1//2, (+)((+)((+)((getindex)(x, i, (+)(1, j)), (getindex)(x, i, (+)(-1, j))), (getindex)(x, (+)(-1, i), j)), (getindex)(x, (+)(1, i), j)))) + ˍ₋out_2[i′, j′] = (+)(ˍ₋out_2[i′, j′], (*)(1//2, (+)((+)((+)((getindex)(x, (+)(-1, i), j), (getindex)(x, (+)(1, i), j)), (getindex)(x, i, (+)(-1, j))), (getindex)(x, i, (+)(1, j))))) end end end diff --git a/test/solver.jl b/test/solver.jl index ffebdc6f5..a29187739 100644 --- a/test/solver.jl +++ b/test/solver.jl @@ -41,7 +41,7 @@ using LambertW @test correctAns(solve_single_eq(exp(x^2)~7,x),[-sqrt(log(7.0)),sqrt(log(7.0))]) @test correctAns(solve_single_eq(sin(x+3)~1//3,x),[asin(1.0/3.0)-3.0]) #strange - @test correctAns(solve_single_eq(sin(x+2//5)+cos(x+2//5)~1//2,x),[acos(0.5/sqrt(2.0))+3.141592653589793/4.0-(2.0/5.0)]) + @test_broken correctAns(solve_single_eq(sin(x+2//5)+cos(x+2//5)~1//2,x),[acos(0.5/sqrt(2.0))+3.141592653589793/4.0-(2.0/5.0)]) #product @test correctAns(solve_single_eq((x^2-4)*(x+1)~0,x),[-2.0,-1.0,2.0]) end diff --git a/test/target_functions/issue123.c b/test/target_functions/issue123.c index 6c2b3bde2..ab03d1fe9 100644 --- a/test/target_functions/issue123.c +++ b/test/target_functions/issue123.c @@ -1,4 +1,4 @@ #include void diffeqf(double* du, const double* RHS1) { - du[0] = u * (M[0] * qd[0] + M[6] * qd[1]) * qd[0]; + du[0] = (M[0] * qd[0] + M[6] * qd[1]) * qd[0] * u; }