Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document LiteralReal #977

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/src/manual/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
2 changes: 1 addition & 1 deletion test/build_function_tests/stencil-extents-inplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/build_function_tests/stencil-extents-outplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/target_functions/issue123.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <math.h>
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;
}
Loading