-
Notifications
You must be signed in to change notification settings - Fork 67
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
Added customized function (a little ugly) #101
base: master
Are you sure you want to change the base?
Conversation
Math.NET Symbolics | ||
Math.NET Symbolics (With Matrix/Vector/Customized Function supported) | ||
|
||
2022-03-12 Now there is a private repo which supports DiffSharp Tensor within Math.NET Symbolics. (very rough/early stage) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add small interesting functions to support Vector, Matrix, Tensor values.
The code would look like this:
open System
open MathNet.Numerics.LinearAlgebra
open MathNet.Symbolics
open Definition
open Operators
open VariableSets.Alphabet
let V = FloatingPoint.RealVector <| vector[1.0;2.0;3.0]
let M = FloatingPoint.RealMatrix <|
matrix [[3.0; 0.0; 0.0]
[1.0; 2.0; 0.0]
[0.0; 1.0; 4.0]]
let symbols2 = dict[ "a", V; "m", M ]
type A = {
trivial: bool
}
[<EntryPoint>]
let main argv =
let a0 = SymbolicExpression(Infix.parseOrThrow("a * 2")).Evaluate(symbols2)
printfn "%A" a0.RealVectorValue
let a1 = SymbolicExpression(Infix.parseOrThrow("a + 1")).Evaluate(symbols2)
printfn "%A" a1.RealVectorValue
let a2 = SymbolicExpression(Infix.parseOrThrow("mat_by_row(a, a)")).Evaluate(symbols2)
printfn "%A" a2.RealMatrixValue
let a3 = SymbolicExpression(Infix.parseOrThrow("mat_by_col(a, a)")).Evaluate(symbols2)
printfn "%A" a3.RealMatrixValue
let a4 = SymbolicExpression(Infix.parseOrThrow("mat_multiply(m, mat_by_col(a, vec(1.0,2.0,3.0), a), a)")).Evaluate(symbols2)
printfn "%A" a4
cFun ("mat_by_row", []) |> ignore
let symV = Symbol "v"
let symW = Symbol "w"
let syml = dict[ "x", FloatingPoint.Real 9.0; ]
let _ = define "t0" ([symV; symW], (v + w))
printfn "t0: %A" <| SymbolicExpression(cFun("t0", [x; x])).Evaluate(syml)
let _ = define "t1" ([symV; symW], Infix.parseOrThrow("t0(v, w)"))
printfn "2 * t1(x, t1(x, x)) / t1(2 * x, x) * 4: %A" <| SymbolicExpression.Parse("2 * t1(x, t1(x, x)) / t1(2 * x, x) * 4").Evaluate(syml)
let _ = define "t2" ([symV; symW], Infix.parseOrThrow("2 * t0(v, w) / 3"))
printfn "2 * t2(x, x) / 3 + t2(x, x * 2): %A" <| SymbolicExpression.Parse("2 * t2(x, x) / 3 + t2(x, x * 2)").Evaluate(syml)
printfn "t1(x, 2 * t0(x,x)): %A" <| SymbolicExpression(cFun("t1", [x; 2 * cFun("t0", [x; x])])).Evaluate(syml)
printfn "t1(x, 2 * t1(x,x)): %A" <| SymbolicExpression(cFun("t1", [x; 2 * cFun("t1", [x; x])])).Evaluate(syml)
printfn "t0(x, t0(x, x) * 2): %A" <| SymbolicExpression(cFun("t0", [x; cFun("t0", [x; x]) * 2])).Evaluate(syml)
printfn "t0(x, t1(x, x) * 2): %A" <| SymbolicExpression(cFun("t0", [x; cFun("t1", [x; x]) * 2])).Evaluate(syml)
let a5 = SymbolicExpression(Infix.parseOrThrow("2 * mat_multiply(m, mat_by_col(a, vec(1.0,2.0,3.0), a), a)")).Evaluate(symbols2)
printfn "%A" a5
let a6 = SymbolicExpression.Parse("2 * htensor(lo(lo(lo(vec(1,2,3), vec(4,5,6)), lo(vec(7,8,9), vec(10,11,12)))))").Evaluate(symbols2)
printfn "%A" a6
If you are interested about it, I can add you...
(Why private? Because it is just like a toy and still a lot of works to do... )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Output:
seq [2.0; 4.0; 6.0]
seq [2.0; 3.0; 4.0]
DenseMatrix 2x3-Double
1 2 3
1 2 3
DenseMatrix 3x2-Double
1 1
2 2
3 3
RealVector (seq [18.0; 30.0; 84.0])
t0: Real 18.0
2 * t1(x, t1(x, x)) / t1(2 * x, x) * 4: Real 8.0
2 * t2(x, x) / 3 + t2(x, x * 2): Real 26.0
t1(x, 2 * t0(x,x)): Real 45.0
t1(x, 2 * t1(x,x)): Real 45.0
t0(x, t0(x, x) * 2): Real 45.0
t0(x, t1(x, x) * 2): Real 45.0
RealVector (seq [36.0; 60.0; 168.0])
twl.Length: 1
twl.Length: 2
twl.Length: 2
v.Count: 3
v.Count: 3
twl.Length: 2
v.Count: 3
v.Count: 3
WTensor
(DSTensor
tensor([[[[ 2., 4., 6.],
[ 8., 10., 12.]],
[[14., 16., 18.],
[20., 22., 24.]]]]))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Why private? Because it is just like a toy and still a lot of works to do... )
Then, it should not be mentioned in ReadMe. Feel free to create issue though.
Actually I never thought this PR would be merged. Just let some one who
needs things like those should not appear in README to know there is a workaround.
…On Sat, Mar 19, 2022, 15:03 FoggyFinder ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In README.md
<#101 (comment)>
:
> @@ -1,6 +1,77 @@
-Math.NET Symbolics
+Math.NET Symbolics (With Matrix/Vector/Customized Function supported)
+
+2022-03-12 Now there is a private repo which supports DiffSharp Tensor within Math.NET Symbolics. (very rough/early stage)
(Why private? Because it is just like a toy and still a lot of works to
do... )
Then, it should not be mentioned in ReadMe. Feel free to create issue
though.
—
Reply to this email directly, view it on GitHub
<#101 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABAXFCO4DBTWVRTFOIJYRDLVAV33LANCNFSM5PNIX3HQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
For supporting code like the following:
Result:
Code:
Result: