From 99f5b750bd15260013276a6542b1bb176bc0818e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 27 Feb 2022 01:09:02 +0800 Subject: [PATCH 01/14] customized_function --- src/Symbolics/Evaluate.fs | 16 ++++++++++++++++ src/Symbolics/Expression.fs | 18 +++++++++++++++++- src/Symbolics/Symbolics.fsproj | 1 + src/Symbolics/Visual/Infix.fs | 15 ++++++++++++--- src/Symbolics/Visual/VisualExpression.fs | 9 +++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/Symbolics/Evaluate.fs b/src/Symbolics/Evaluate.fs index b1d1050..193f2de 100644 --- a/src/Symbolics/Evaluate.fs +++ b/src/Symbolics/Evaluate.fs @@ -228,6 +228,8 @@ module Evaluate = | HankelH2, [Real nu; Complex x] -> Complex (SpecialFunctions.HankelH2 (nu, x)) | _ -> failwith "not supported" + + [] let rec evaluate (symbols:IDictionary) = function | Number n -> Real (float n) |> fnormalize @@ -250,3 +252,17 @@ module Evaluate = | Power (r, p) -> fpower (evaluate symbols r) (evaluate symbols p) |> fnormalize | Function (f, x) -> fapply f (evaluate symbols x) |> fnormalize | FunctionN (f, xs) -> xs |> List.map (evaluate symbols) |> fapplyN f |> fnormalize + | FunInvocation (Symbol fnm, xs) -> + let param, fx = Definition.funDict.[fnm] + let cmpl = Compile.compileExpressionOrThrow fx param + + let param_val = + xs + |> List.map (evaluate symbols) + |> List.map (fun pv -> + match pv with + | (FloatingPoint.Real v) -> box v + | _ -> null + ) + |> Array.ofList + cmpl.DynamicInvoke(param_val:obj[]) :?> float |> Real diff --git a/src/Symbolics/Expression.fs b/src/Symbolics/Expression.fs index 39faed3..daa235b 100644 --- a/src/Symbolics/Expression.fs +++ b/src/Symbolics/Expression.fs @@ -15,12 +15,13 @@ type Expression = | Power of Expression * Expression | Function of Function * Expression | FunctionN of FunctionN * (Expression list) - //| FunctionDef of Symbol * (Symbol list) * Expression + | FunInvocation of Symbol * (Expression list) | ComplexInfinity | PositiveInfinity | NegativeInfinity | Undefined + [] module Values = @@ -182,6 +183,20 @@ module Operators = | Power (xr,xp), Power (yr,yp) -> if xr <> yr then compare xr yr else compare xp yp | Function (xf, x), Function (yf, y) -> if xf <> yf then xf < yf else compare x y | FunctionN (xf, xs), FunctionN (yf, ys) -> if xf <> yf then xf < yf else compareZip (List.rev xs) (List.rev ys) + | FunInvocation (name1, paramdef1), FunInvocation (name2, paramdef2) -> + if name1 <> name2 then name1 < name2 + else + let l1 = List.length paramdef1 + let l2 = List.length paramdef2 + if l1 <> l2 then l1 < l2 + else + (paramdef1, paramdef2) + ||> List.map2 (fun p1 p2 -> + compare p1 p2 + ) + |> List.find (fun result -> not result) + + | Number _, _ -> true | _, Number _ -> false | Approximation _, _ -> true @@ -395,6 +410,7 @@ module Operators = let private PiI = multiply Pi I let private PiIHalf = divide PiI two + let cFun(fnm, paramList) = FunInvocation (Symbol fnm, paramList) let abs : Expression -> Expression = function | Undefined -> undefined | oo when isInfinity oo -> infinity diff --git a/src/Symbolics/Symbolics.fsproj b/src/Symbolics/Symbolics.fsproj index 8488986..7d3bf7d 100644 --- a/src/Symbolics/Symbolics.fsproj +++ b/src/Symbolics/Symbolics.fsproj @@ -24,6 +24,7 @@ Minor internal simplifications and streamlining + diff --git a/src/Symbolics/Visual/Infix.fs b/src/Symbolics/Visual/Infix.fs index 6d91998..b09fde3 100644 --- a/src/Symbolics/Visual/Infix.fs +++ b/src/Symbolics/Visual/Infix.fs @@ -60,10 +60,18 @@ module private InfixParser = let functionArgs = sepBy expr (str_ws ",") |> between (str_ws "(") (str_ws ")") let functionTerm = symbolName .>>. functionArgs |>> function - | f, args -> VisualExpression.Function (f, BigInteger.One, args) + | f, args -> + if Definition.funDict.ContainsKey f then + VisualExpression.FunInvocation (f, BigInteger.One, args) + else + VisualExpression.Function (f, BigInteger.One, args) let functionPowerTerm = symbolName .>>. (str_ws "^" >>. integer) .>>. functionArgs |>> function - | (f, power), args -> VisualExpression.Function (f, power, args) + | (f, power), args -> + if Definition.funDict.ContainsKey f then + VisualExpression.FunInvocation (f, BigInteger.One, args) + else + VisualExpression.Function (f, power, args) let sqrtTerm = str_ws "sqrt" >>. (between (str_ws "(") (str_ws ")") expr) |>> function | arg -> VisualExpression.Root (arg, bigint 2) @@ -181,7 +189,8 @@ module private InfixFormatter = write "(" format write x write ")" - | VisualExpression.Function (fn, power, x::xs) -> + | VisualExpression.Function (fn, power, x::xs) + | VisualExpression.FunInvocation (fn, power, x::xs) -> write fn if power.IsOne |> not then write "^" diff --git a/src/Symbolics/Visual/VisualExpression.fs b/src/Symbolics/Visual/VisualExpression.fs index b97e4e9..12b99ef 100644 --- a/src/Symbolics/Visual/VisualExpression.fs +++ b/src/Symbolics/Visual/VisualExpression.fs @@ -22,6 +22,7 @@ type VisualExpression = | Power of VisualExpression * VisualExpression // a^b | Root of VisualExpression * BigInteger // a^(1/b) | Function of name:string * power:BigInteger * (VisualExpression list) + | FunInvocation of name:string * power:BigInteger * (VisualExpression list) | ComplexI | RealPi | RealE @@ -204,6 +205,8 @@ module VisualExpression = | FunctionN (f, xs) -> VisualExpression.Function (functionNaryName f, BigInteger.One, xs |> List.map (convert 0)) |> parenthesis priority 3 + | FunInvocation (Symbol fnm, xs) -> + VisualExpression.FunInvocation (fnm, BigInteger.One, xs |> List.map (convert 0)) convert 0 expression let toExpression visualExpression = @@ -219,6 +222,12 @@ module VisualExpression = | VisualExpression.Fraction (numerator, denominator) -> (convert numerator)/(convert denominator) | VisualExpression.Power (radix, power) -> pow (convert radix) (convert power) | VisualExpression.Root (radix, power) -> root (fromInteger power) (convert radix) + | VisualExpression.FunInvocation (fn, power, xs) -> + let paramExp = xs |> List.map convert + let applied = Expression.FunInvocation ((Symbol fn), paramExp) + if power.IsOne then + applied + else pow applied (fromInteger power) | VisualExpression.Function (fn, power, [x]) -> let applied = apply (nameFunction fn) (convert x) if power.IsOne then applied else pow applied (fromInteger power) From 3822c760a6920be5fc6eab2b0d20725a3e5c8e71 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 27 Feb 2022 01:11:36 +0800 Subject: [PATCH 02/14] customized_function --- src/Symbolics/Definition.fs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/Symbolics/Definition.fs diff --git a/src/Symbolics/Definition.fs b/src/Symbolics/Definition.fs new file mode 100644 index 0000000..78be50f --- /dev/null +++ b/src/Symbolics/Definition.fs @@ -0,0 +1,12 @@ +namespace MathNet.Symbolics + +module Definition = + + let funDict = new System.Collections.Concurrent.ConcurrentDictionary() + + let define fnm exp = + funDict.AddOrUpdate( + fnm + , (fun nm -> exp) + , (fun nm cur_exp -> exp) + ) From 3fbc1f107706abe43e1e90a4f2b3b0274cce3577 Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 28 Feb 2022 16:39:02 +0800 Subject: [PATCH 03/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f09685..26eece7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Math.NET Symbolics +Math.NET Symbolics (With Matrix/Vector supported) ================== Math.NET Symbolics is a basic open source **computer algebra library for .NET, Silverlight and Mono** written entirely in F#. From e7e8ec771ad6b74fa46fce59efab9c8e4152f7ec Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 28 Feb 2022 16:40:03 +0800 Subject: [PATCH 04/14] Update README.md --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 26eece7..7502ed3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,25 @@ Math.NET Symbolics (With Matrix/Vector supported) ================== +``` +open System +open MathNet.Numerics.LinearAlgebra +open MathNet.Symbolics +let v = FloatingPoint.RealVector <| vector[1.0;2.0;3.0] + +let symbols2 = dict[ "a", v ] + +[] +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 + 0 // return an integer exit code +``` + Math.NET Symbolics is a basic open source **computer algebra library for .NET, Silverlight and Mono** written entirely in F#. This project does *not* aim to become a full computer algebra system. If you need such a system, have a look at Axiom or Maxima instead, or for proprietary commercial solutions Maple, Mathematica or Wolfram Alpha. From 5c21caf5ade2c594a29220ffa4b05bdc9c346221 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 28 Feb 2022 16:43:54 +0800 Subject: [PATCH 05/14] Added vector/matrix support --- .../Compilation/Compilation.fs | 3 + src/Symbolics/Definition.fs | 43 ++++++++++++- src/Symbolics/Evaluate.fs | 60 ++++++++++++++++--- src/Symbolics/Typed/Linq.fs | 2 +- 4 files changed, 96 insertions(+), 12 deletions(-) diff --git a/src/Symbolics.Tests/Compilation/Compilation.fs b/src/Symbolics.Tests/Compilation/Compilation.fs index c5e7045..14071aa 100644 --- a/src/Symbolics.Tests/Compilation/Compilation.fs +++ b/src/Symbolics.Tests/Compilation/Compilation.fs @@ -44,6 +44,9 @@ module Compilation = let expr9 = x + 1 (Compile.compileExpression1OrThrow expr9 symX).Invoke(1.0) --> 2.0 + let expr10' = y * y + Expression.I * y + (besselk x (Expression.I * y)) / (besselk (negate x) (Expression.I * y)) + (Compile.compileComplexExpression2OrThrow expr10' symX symY).Invoke(toComplex 0.5, System.Numerics.Complex.One) --> complex 2.0 1.0 + let expr1' = x (Compile.compileComplexExpression1OrThrow expr1' symX).Invoke(toComplex 3.0) --> toComplex 3.0 diff --git a/src/Symbolics/Definition.fs b/src/Symbolics/Definition.fs index 78be50f..1c7ccfc 100644 --- a/src/Symbolics/Definition.fs +++ b/src/Symbolics/Definition.fs @@ -2,11 +2,48 @@ module Definition = - let funDict = new System.Collections.Concurrent.ConcurrentDictionary() + type DefType = + | DTExp of (Symbol list) * Expression + | DTFunAction of (unit -> unit) + | DTFunI1toI1 of (int -> int) + | KeyWord + + let funDict = new System.Collections.Concurrent.ConcurrentDictionary() + + let kwlist = [ "vec", true; "mat_by_row", true; "mat_by_col", true ] + + let keyWord = dict kwlist + + kwlist |> List.iter (fun (k, _) -> funDict.TryAdd(k, KeyWord) |> ignore) + + let defineSafe fnm exp = + if keyWord.ContainsKey fnm then + failwith "used function name" + funDict.TryAdd (fnm, DTExp exp) let define fnm exp = + if keyWord.ContainsKey fnm then + failwith "used function name" + funDict.AddOrUpdate( + fnm + , (fun nm -> DTExp exp) + , (fun nm cur_exp -> DTExp exp) + ) + + let defAct fnm f = + if keyWord.ContainsKey fnm then + failwith "used function name" + funDict.AddOrUpdate( + fnm + , (fun nm -> DTFunAction f) + , (fun nm cur_exp -> DTFunAction f) + ) + + let def1ito1i fnm f = + if keyWord.ContainsKey fnm then + failwith "used function name" funDict.AddOrUpdate( fnm - , (fun nm -> exp) - , (fun nm cur_exp -> exp) + , (fun nm -> DTFunI1toI1 f) + , (fun nm cur_exp -> DTFunI1toI1 f) ) diff --git a/src/Symbolics/Evaluate.fs b/src/Symbolics/Evaluate.fs index 193f2de..e7c6cf1 100644 --- a/src/Symbolics/Evaluate.fs +++ b/src/Symbolics/Evaluate.fs @@ -5,6 +5,7 @@ open System.Collections.Generic open MathNet.Numerics open MathNet.Numerics.LinearAlgebra open MathNet.Symbolics +open Definition [] type FloatingPoint = @@ -84,6 +85,7 @@ module Evaluate = | Real x, Real y -> Real (x+y) | Real x, Complex y | Complex y, Real x -> Complex ((complex x 0.0) + y) | Complex x, Complex y -> Complex (x+y) + | Real x, RealVector y -> RealVector (x+y) | RealVector x, RealVector y -> RealVector (x+y) | ComplexVector x, ComplexVector y -> ComplexVector (x+y) | RealMatrix x, RealMatrix y -> RealMatrix (x+y) @@ -100,6 +102,7 @@ module Evaluate = | Real x, Real y -> Real (x*y) | Real x, Complex y | Complex y, Real x -> Complex ((complex x 0.0) * y) | Complex x, Complex y -> Complex (x*y) + | Real x, RealVector y -> RealVector (x*y) | RealVector x, RealVector y -> Real (x*y) | ComplexVector x, ComplexVector y -> Complex (x*y) | RealMatrix x, RealMatrix y -> RealMatrix (x*y) @@ -253,16 +256,57 @@ module Evaluate = | Function (f, x) -> fapply f (evaluate symbols x) |> fnormalize | FunctionN (f, xs) -> xs |> List.map (evaluate symbols) |> fapplyN f |> fnormalize | FunInvocation (Symbol fnm, xs) -> - let param, fx = Definition.funDict.[fnm] - let cmpl = Compile.compileExpressionOrThrow fx param - - let param_val = + let cal_param_obj_val () = xs - |> List.map (evaluate symbols) - |> List.map (fun pv -> - match pv with + |> List.map (fun exp -> + match evaluate symbols exp with | (FloatingPoint.Real v) -> box v | _ -> null ) |> Array.ofList - cmpl.DynamicInvoke(param_val:obj[]) :?> float |> Real + let cal_param_real_val () = + xs + |> List.map (fun exp -> + match evaluate symbols exp with + | (FloatingPoint.Real v) -> v + | _ -> Double.NaN + ) + |> Array.ofList + let cal_param_vec_val () = + xs + |> List.map (fun exp -> + match evaluate symbols exp with + | (FloatingPoint.RealVector v) -> v + | _ -> failwithf "vector parameter is required for %s" fnm + ) + |> Array.ofList + if keyWord.ContainsKey fnm then + let mbr () = + let param_val = cal_param_vec_val () + let m2 = DenseMatrix.zero (param_val.Length) (param_val.[0].Count) + param_val + |> Array.iteri (fun i v -> + m2.SetRow(i, v) + ) + m2 + match fnm with + | "vec" -> + let param_val = cal_param_real_val () + FloatingPoint.RealVector <| vector param_val + | "mat_by_row" -> + FloatingPoint.RealMatrix (mbr ()) + | "mat_by_col" -> + let m2 = mbr() + FloatingPoint.RealMatrix <| m2.Transpose() + else + match funDict.[fnm] with + | DTExp (param, fx) -> + let param_val = cal_param_obj_val () + let cmpl = Compile.compileExpressionOrThrow fx param + cmpl.DynamicInvoke(param_val:obj[]) :?> float |> Real + | DTFunI1toI1 f -> + let param_val = cal_param_real_val () + f (int param_val.[0]) |> float |> Real + + + diff --git a/src/Symbolics/Typed/Linq.fs b/src/Symbolics/Typed/Linq.fs index d6630f2..00ff18e 100644 --- a/src/Symbolics/Typed/Linq.fs +++ b/src/Symbolics/Typed/Linq.fs @@ -233,7 +233,7 @@ module Linq = let constant = function | E -> Some (Expression.Constant (complex Constants.E 0.0) :> Expression) | Pi -> Some (Expression.Constant (complex Constants.Pi 0.0) :> Expression) - | _ -> None + | I -> Some (Expression.Constant (complex 0.0 1.0) :> Expression) let valueType = typeof let mathType = typeof let mathCall1 (name : string) (a : Expression) = Expression.Call(mathType.GetMethod(name, [| valueType |]), a) :> Expression From baecc8b1c748f7a133830491ec9836fb55c23d56 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 28 Feb 2022 16:47:10 +0800 Subject: [PATCH 06/14] Added vector/matrix debug project --- debugger/Program.fs | 18 ++++++++++++++++++ debugger/debugger.fsproj | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 debugger/Program.fs create mode 100644 debugger/debugger.fsproj diff --git a/debugger/Program.fs b/debugger/Program.fs new file mode 100644 index 0000000..050c804 --- /dev/null +++ b/debugger/Program.fs @@ -0,0 +1,18 @@ +// Learn more about F# at http://fsharp.org + +open System +open MathNet.Numerics.LinearAlgebra +open MathNet.Symbolics +let v = FloatingPoint.RealVector <| vector[1.0;2.0;3.0] + +let symbols2 = dict[ "a", v ] + +[] +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 + 0 // return an integer exit code diff --git a/debugger/debugger.fsproj b/debugger/debugger.fsproj new file mode 100644 index 0000000..6bc253a --- /dev/null +++ b/debugger/debugger.fsproj @@ -0,0 +1,16 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + + From 2108a81f17b9cb254909b90e62ee1c0d62e07e2b Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 28 Feb 2022 16:49:23 +0800 Subject: [PATCH 07/14] Update README.md --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7502ed3..a71e92a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,52 @@ -Math.NET Symbolics (With Matrix/Vector supported) +Math.NET Symbolics (With Matrix/Vector/Customized Function supported) ================== +For supporting code like the following: + +``` +#r @"src\Symbolics\bin\Debug\netstandard2.0\MathNet.Symbolics.dll" +#r @"nuget:MathNet.Numerics" +#r @"nuget:FsUnit" +#r @"nuget:FParsec" +#r @"nuget:MathNet.Numerics.FSharp" +#load @"src\Symbolics.Tests\Global.fs" + +open MathNet.Numerics +open MathNet.Symbolics +open Global +open Operators +open VariableSets.Alphabet +type Expr = SymbolicExpression + +let symV = Symbol "v" +let symW = Symbol "w" +let symX = Symbol "x" +let symY = Symbol "y" +let symZ = Symbol "z" + + +open Definition +define "test" ([symV; symW], (v + w)*2) +SymbolicExpression(Infix.parseOrThrow("2^test(x, 2 * x)")).Evaluate(dict[ "x", FloatingPoint.Real 2.0; ]) +``` + +Result: +``` +val it : FloatingPoint = Real 4096.0 +``` + +Code: +``` +SymbolicExpression(cFun("test", [x + (fromInt32 10); (fromDouble 100.0)])*2).Evaluate(dict[ "x", FloatingPoint.Real 9.0; ]) + +``` + +Result: +``` +val it : FloatingPoint = Real 476.0 +``` + + ``` open System open MathNet.Numerics.LinearAlgebra From 5ef25bd6a23c1d93820aa648a86d58f7dbb97209 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 28 Feb 2022 17:24:32 +0800 Subject: [PATCH 08/14] matrix/vector multiplication --- README.md | 2 ++ src/Symbolics/Evaluate.fs | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/README.md b/README.md index a71e92a..d3b80ac 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,8 @@ let main argv = printfn "%A" a1.RealVectorValue let a2 = SymbolicExpression(Infix.parseOrThrow("mat_by_row(a, a)")).Evaluate(symbols2) printfn "%A" a2.RealMatrixValue + let a4 = SymbolicExpression(Infix.parseOrThrow("mat_multiply(m, m, a)")).Evaluate(symbols2) + printfn "%A" a4 0 // return an integer exit code ``` diff --git a/src/Symbolics/Evaluate.fs b/src/Symbolics/Evaluate.fs index e7c6cf1..4d57812 100644 --- a/src/Symbolics/Evaluate.fs +++ b/src/Symbolics/Evaluate.fs @@ -280,6 +280,15 @@ module Evaluate = | _ -> failwithf "vector parameter is required for %s" fnm ) |> Array.ofList + let cal_param_mat_vec_val () = + xs + |> List.map (fun exp -> + match evaluate symbols exp with + | (FloatingPoint.RealVector v) -> FloatingPoint.RealVector v + | (FloatingPoint.RealMatrix v) -> FloatingPoint.RealMatrix v + | _ -> failwithf "vector parameter is required for %s" fnm + ) + |> Array.ofList if keyWord.ContainsKey fnm then let mbr () = let param_val = cal_param_vec_val () @@ -298,6 +307,45 @@ module Evaluate = | "mat_by_col" -> let m2 = mbr() FloatingPoint.RealMatrix <| m2.Transpose() + | "mat_multiply" -> + let param_val = cal_param_mat_vec_val () + param_val + |> Array.skip 1 + |> Array.fold (fun s a -> + match s with + | FloatingPoint.RealVector vs -> + match a with + | FloatingPoint.RealVector va -> + let r = vs * va + FloatingPoint.Real r + | FloatingPoint.RealMatrix ma -> + let r = vs * ma + FloatingPoint.RealVector r + | FloatingPoint.Real ra -> + FloatingPoint.RealVector (vs * ra) + | FloatingPoint.RealMatrix ms -> + match a with + | FloatingPoint.RealVector va -> + let r = ms * va + FloatingPoint.RealVector r + | FloatingPoint.RealMatrix ma -> + let r = ms * ma + FloatingPoint.RealMatrix r + | FloatingPoint.Real ra -> + let r = ra * ms + FloatingPoint.RealMatrix r + | FloatingPoint.Real rs -> + match a with + | FloatingPoint.RealVector va -> + let r = rs * va + FloatingPoint.RealVector r + | FloatingPoint.RealMatrix ma -> + let r = rs * ma + FloatingPoint.RealMatrix r + | FloatingPoint.Real ra -> + let r = ra * rs + FloatingPoint.Real r + ) param_val.[0] else match funDict.[fnm] with | DTExp (param, fx) -> From 829c4da44c3f7d05c2802b3d0f9ae925c228db48 Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 28 Feb 2022 17:25:54 +0800 Subject: [PATCH 09/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d3b80ac..d957ee8 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ let main argv = printfn "%A" a1.RealVectorValue let a2 = SymbolicExpression(Infix.parseOrThrow("mat_by_row(a, a)")).Evaluate(symbols2) printfn "%A" a2.RealMatrixValue - let a4 = SymbolicExpression(Infix.parseOrThrow("mat_multiply(m, m, a)")).Evaluate(symbols2) + let a4 = SymbolicExpression(Infix.parseOrThrow("mat_multiply(m, m, a)")).Evaluate(symbols2) printfn "%A" a4 0 // return an integer exit code ``` From 92a8e5b59115c1ff084f40249e7043de976e5f89 Mon Sep 17 00:00:00 2001 From: ingted Date: Sat, 12 Mar 2022 23:07:20 +0800 Subject: [PATCH 10/14] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index d957ee8..7f154cb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ 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) +If you are interested about it, please leave a message in issues. + ================== For supporting code like the following: From 5617b8de2971d2d30ae04b8d4231341d25a0aa01 Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 21 Mar 2022 18:41:04 +0800 Subject: [PATCH 11/14] Update README.md --- README.md | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/README.md b/README.md index 7f154cb..36064e8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,138 @@ Math.NET Symbolics (With Matrix/Vector/Customized Function supported) +2022-03-21 The progress is really great. Critical lambda compilation and evaluation bugs are resolved. + +``` + let a0 = SymbolicExpression(Infix.parseOrThrow("v * 2")).Evaluate(symbols2) + printfn "%A" a0.RealVectorValue + let a1 = SymbolicExpression(Infix.parseOrThrow("v + 1")).Evaluate(symbols2) + printfn "%A" a1.RealVectorValue + let a2 = SymbolicExpression(Infix.parseOrThrow("mat_by_row(v, v)")).Evaluate(symbols2) + printfn "%A" a2.RealMatrixValue + let a3 = SymbolicExpression(Infix.parseOrThrow("mat_by_col(v, v)")).Evaluate(symbols2) + printfn "a3: %A" a3.RealMatrixValue + let a4 = SymbolicExpression(Infix.parseOrThrow("mat_multiply(m, mat_by_col(v, vec(1.0,2.0,3.0), v), v)")).Evaluate(symbols2) + printfn "a4: %A" a4 + + cFun ("mat_by_row", []) |> ignore + + let symV = Symbol "v" + let symW = Symbol "w" + + let symV1 = Symbol "v1" + let symW1 = Symbol "w1" + let symV2 = Symbol "v2" + let symW2 = Symbol "w2" + let symX = Symbol "x" + let syml = dict [ "x", FloatingPoint.Real 9.0; ] + let _ = define "t0" ([symV; symW], (v + w)) + let _ = define "t1" ([symV; symW], Infix.parseOrThrow("t0(v, w)")) + let _ = define "t2" ([symV; symW], Infix.parseOrThrow("2 * t0(v, w) / 3")) + + let lambdaExp = + try + MathNet.Symbolics.Linq.formatLambda (cFun("t0", [x; x])) [symV; symW] //intensive error + with + | _ -> None + + printfn "t0: %A" <| SymbolicExpression(cFun("t0", [x; x])).Evaluate(syml) + printfn "t0-2: %A" <| SymbolicExpression.Parse("1 + t0(x, x)").Evaluate(syml) + 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 infix2 = Infix.parseOrThrow("2 * t0(v1, w1) / 3") + let lambdaExp2 = + try + MathNet.Symbolics.Linq.formatLambda infix2 [symV2; symW2] //intensive error + with + | _ -> None + + let infix3_0 = Infix.parseOrThrow("t0(x, x)") + let infix3_1 = Infix.parseOrThrow("t1(x, x)") + let infix3_2 = Infix.parseOrThrow("t2(x, x * 2)") + let infix3 = Infix.parseOrThrow("2 * t2(x, x) / 3 + t2(x, x * 2)") + + + let (Some lambdaExp3_0) = MathNet.Symbolics.Linq.formatLambda infix3_0 [symX] + let (Some lambdaExp3_2) = MathNet.Symbolics.Linq.formatLambda infix3_2 [symX] + let (Some lambdaExp3) = MathNet.Symbolics.Linq.formatLambda infix3 [symX] + + let toEvaluate = SymbolicExpression.Parse("2 * t2(x, x) / 3 + t2(x, x * 2)") + let (Some toLambda) = MathNet.Symbolics.Linq.formatLambda toEvaluate.Expression [symX] + + printfn "2 * t2(x, x) / 3 + t2(x, x * 2): %A" <| toEvaluate.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(v, vec(1.0,2.0,3.0), v), v)")).Evaluate(symbols2) + printfn "a5: %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 "a6:%A" a6 + + let a7expr = SymbolicExpression.Parse("t0(1, 2 * htensor(lo(lo(lo(vec(1,2,3), vec(4,5,6)), lo(vec(7,8,9), vec(10,11,12))))))") + let a7 = a7expr.Evaluate(symbols2) + printfn "a7:%A" a7 +``` +Results: +``` +seq [2.0; 4.0; 6.0] +seq [2.0; 3.0; 4.0] +DenseMatrix 2x3-Double +1 2 3 +1 2 3 + +a3: DenseMatrix 3x2-Double +1 1 +2 2 +3 3 + +a4: RealVector (seq [18.0; 30.0; 84.0]) +t0: Real 18.0 +t0-2: Real 19.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 +a5: 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 +a6:WTensor + (DSTensor + tensor([[[[ 2., 4., 6.], + [ 8., 10., 12.]], + + [[14., 16., 18.], + [20., 22., 24.]]]])) +twl.Length: 1 +twl.Length: 2 +twl.Length: 2 +v.Count: 3 +v.Count: 3 +twl.Length: 2 +v.Count: 3 +v.Count: 3 +a7:WTensor + (DSTensor + tensor([[[[ 3., 5., 7.], + [ 9., 11., 13.]], + + [[15., 17., 19.], + [21., 23., 25.]]]])) +``` + + + + + 2022-03-12 Now there is a private repo which supports DiffSharp Tensor within Math.NET Symbolics. (very rough/early stage) If you are interested about it, please leave a message in issues. From 46d699a11f9e1b2af9fa0159639f06ebe55c2699 Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 21 Mar 2022 18:41:19 +0800 Subject: [PATCH 12/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 36064e8..d0b91cb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ Math.NET Symbolics (With Matrix/Vector/Customized Function supported) -2022-03-21 The progress is really great. Critical lambda compilation and evaluation bugs are resolved. +# 2022-03-21 The progress is really great. Critical lambda compilation and evaluation bugs are resolved. ``` let a0 = SymbolicExpression(Infix.parseOrThrow("v * 2")).Evaluate(symbols2) From 784b37d86a33f8baa48f5d99e7a8d42bae92c80b Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 21 Mar 2022 18:42:27 +0800 Subject: [PATCH 13/14] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index d0b91cb..27b9663 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ Math.NET Symbolics (With Matrix/Vector/Customized Function supported) +# if you are interested about it, send me an email to join this repo: + +https://github.com/ingted/coldfar-symbolics + # 2022-03-21 The progress is really great. Critical lambda compilation and evaluation bugs are resolved. ``` From b65b408df72a27110ddc97901351539117413c3e Mon Sep 17 00:00:00 2001 From: ingted Date: Mon, 21 Mar 2022 18:44:10 +0800 Subject: [PATCH 14/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27b9663..416b590 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Math.NET Symbolics (With Matrix/Vector/Customized Function supported) https://github.com/ingted/coldfar-symbolics -# 2022-03-21 The progress is really great. Critical lambda compilation and evaluation bugs are resolved. +# 2022-03-21 The progress is really great. Critical lambda compilation and evaluation bugs are resolved. So you can easily extend symbolic operation with tensor value in Math.Net. ``` let a0 = SymbolicExpression(Infix.parseOrThrow("v * 2")).Evaluate(symbols2)