You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
y ^ 2 + x & ModelingToolkit.Constant(0) & ModelingToolkit.Constant(0) \\
45
+
(x ^ 2) + y & 0 & 2 * x \\
46
+
0 & 0 & 2 * y \\
47
+
(y ^ 2) + x & 0 & 0 \\
48
48
\end{array}
49
49
\right]
50
50
\end{equation}
@@ -58,10 +58,10 @@ using SparseArrays
58
58
spA =sparse(A)
59
59
60
60
3×3 SparseMatrixCSC{Expression,Int64} with 4 stored entries:
61
-
[1, 1] = x ^2+ y
62
-
[3, 1] = y ^2+ x
63
-
[1, 3] =2x
64
-
[2, 3] =2y
61
+
[1, 1] =(x ^2)+ y
62
+
[3, 1] =(y ^2)+ x
63
+
[1, 3] =2* x
64
+
[2, 3] =2* y
65
65
```
66
66
67
67
We can thus use normal Julia functions as generators for sparse
@@ -73,7 +73,7 @@ function f(u)
73
73
end
74
74
f([x,y,z]) # Recall that z = x^2 + y
75
75
76
-
3-element Array{Operation,1}:
76
+
3-element Array{Num,1}:
77
77
x - (x ^2+ y)
78
78
x ^2- y
79
79
(x ^2+ y) + y
@@ -85,7 +85,7 @@ Or we can build array variables and use these to trace:
85
85
@variables u[1:3]
86
86
f(u)
87
87
88
-
3-element Array{Operation,1}:
88
+
3-element Array{Num,1}:
89
89
u₁ - u₃
90
90
u₁ ^2- u₂
91
91
u₃ + u₂
@@ -178,7 +178,7 @@ a function via a sparse matrix. For example:
178
178
N =8
179
179
A =sparse(Tridiagonal([x^i for i in1:N-1],[x^i * y^(8-i) for i in1:N], [y^i for i in1:N-1]))
180
180
181
-
8×8 SparseMatrixCSC{Operation,Int64} with 22 stored entries:
181
+
8×8 SparseMatrixCSC{Num,Int64} with 22 stored entries:
182
182
[1, 1] = x ^1* y ^7
183
183
[2, 1] = x ^1
184
184
[1, 2] = y ^1
@@ -338,7 +338,7 @@ This can be applied to arrays by using Julia's broadcast mechanism:
338
338
B =simplify.([t^2+t+t^22t+4t
339
339
x+y+y+2t x^2- x^2+ y^2])
340
340
341
-
2×2 Array{Operation,2}:
341
+
2×2 Array{Num,2}:
342
342
2* t ^2+ t 6t
343
343
x +2* (t + y) y ^2
344
344
```
@@ -348,7 +348,7 @@ We can then use `substitute` to change values of an expression around:
348
348
```julia
349
349
simplify.(substitute.(B,[x=>y^2]))
350
350
351
-
2×2 Array{Operation,2}:
351
+
2×2 Array{Num,2}:
352
352
2* t ^2+ t 6t
353
353
y ^2+2* (t + y) y ^2
354
354
```
@@ -380,8 +380,8 @@ the user's code. For these cases, ModelingToolkit.jl allows for fully
380
380
macro-free usage. For example:
381
381
382
382
```julia
383
-
x =Variable{Float64}(:x)()
384
-
y =Variable{Float64}(:y)()
383
+
x =Sym{Float64}(:x)()
384
+
y =Sym{Float64}(:y)()
385
385
x+y^2.0
386
386
```
387
387
@@ -396,23 +396,17 @@ convert the output to an `Expr`:
396
396
Expr(x+y^2)
397
397
```
398
398
399
-
## Variables as Operations
399
+
## `Sym`s and callable `Sym`s
400
400
401
-
`Operation` is the type name the ModelingToolkit.jl gives to symbolic
402
-
expressions. In ModelingToolkit.jl, essentially everything is an
403
-
`Operation`. Notice that when we defined our variables above, they
404
-
were represented as an `Operation` as well, which means that variables
405
-
alone are an operation that can then be composed to make bigger
406
-
operations.
407
-
408
-
But since variables are functions, we can represent their dependencies
409
-
as well. For example:
401
+
In the definition
410
402
411
403
```julia
412
404
@variables t x(t) y(t)
413
405
```
414
406
415
-
defines `t` as a dependent variable while `x(t)` and `y(t)` are
407
+
`t` is of type `Sym{Real}` but the name `x` refers to an object that represents the `Term``x(t)`. The operation of this expression is itself the object `Sym{FnType{Tuple{Real}, Real}}(:x)`. The type `Sym{FnType{...}}` represents a callable object. In this case specifically it's a function that takes 1 Real argument (noted by `Tuple{Real}`) and returns a `Real` result. You can call such a callable `Sym` with either a number or a symbolic expression of a permissible type.
408
+
409
+
this expression also defines `t` as a dependent variable while `x(t)` and `y(t)` are
416
410
independent variables. This is accounted for in differentiation:
0 commit comments