Skip to content
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
4 changes: 4 additions & 0 deletions src/Symbolics/Expression.fs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ module Operators =
| x -> Product (List.rev x)

/// Multiply a number with an expression (potentially a denormalized product)
/// Intentionally distribte -1, e.g. -(a + b) = -a - b
let rec valueMul (v:Value) x =
if Value.isZero v then zero else
match x with
Expand All @@ -306,6 +307,9 @@ module Operators =
| Product [a] -> if Value.isOne v then a else Product [Values.unpack v; a]
| Product ((Values.Value a)::ax) -> valueMul (Value.product (a,v)) (Product ax)
| Product ax -> if Value.isOne v then x else Product (Values.unpack v::ax)
| Sum ax as x'-> if Value.isOne v then x'
else if Value.isMinusOne v then ax |> List.map (function xi -> valueMul v xi) |> Sum
else Product [Values.unpack v; x']
| x -> if Value.isOne v then x else Product [Values.unpack v; x]

match x, y with
Expand Down
2 changes: 1 addition & 1 deletion src/SymbolicsUnitTests/Operators/Exponential.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let tests =
}

test "Special Values" {
exp(x + y - (x + y)) ==> "exp(x + y - (x + y))" // "1"
exp(x + y - (x + y)) ==> "1"

exp(-1Q) ==> "1/e"
exp(1Q/2Q*pi*Constant I) ==> "j"
Expand Down
13 changes: 7 additions & 6 deletions src/SymbolicsUnitTests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,13 @@ let tests =

test "Algebraic Expansion" {

// Auto-simplification does not expand expressions:
(a+b)-(a+b) ==> "a + b - (a + b)"
// Auto-simplification does not expand expressions, but -1 distributes inside parentheses:
(a+b)-(a+b) ==> "0"
(a+b)-(a+b) |> Algebraic.expand ==> "0"
2*(a+b)-(a+b) ==> "a + b"
2*(a+b)-(a+b) ==> "-a - b + 2*(a + b)"
(a+b)-2*(a+b) |> Algebraic.expand ===> "(-1)*a + (-1)*b"
(a+b)-2*(a+b) |> Algebraic.expand ==> "-a - b"
-(a + b) + 2*(a + b) ==> "-a - b + 2*(a + b)"

(a*b)/(b*a) ==> "1"
(a*b)**2/(b*a) ==> "a*b"
Expand Down Expand Up @@ -285,7 +286,7 @@ let tests =

test "Algebaric Operators" {

negate (x + y**2) ==> "-(x + y^2)"
negate (x + y**2) ==> "-x - y^2"

Algebraic.factors (b*cos(x)*ln(d)*x) ==+> ["b"; "x"; "ln(d)"; "cos(x)"]
Algebraic.factors (b*cos(x)*log10(d)*x) ==+> ["b"; "x"; "log(d)"; "cos(x)"]
Expand Down Expand Up @@ -402,7 +403,7 @@ let tests =

// TODO: expected: 0
Trigonometric.simplify (sin(x) + sin(y) - 2*sin(x/2+y/2)*cos(x/2-y/2))
==> "sin(y) - sin(x - y)/2 - sin(x/2 - y/2 - (x/2 - y/2))/2 - sin(-x/2 + y/2 - (x/2 - y/2))/2 - sin(x/2 + y/2 - (x/2 - y/2))"
==> "-sin(-x + y)/2 - sin(x - y)/2" // "0"
}

test "Differentiation and Taylor Series" {
Expand Down Expand Up @@ -482,7 +483,7 @@ let tests =
solve x (2+3*x) ==> "-2/3"

// sin(a)+x*cos(b)+c = 0 --> x =
solve x (sin(a)+x*cos(b)+c) ==> "-(c + sin(a))/cos(b)"
solve x (sin(a)+x*cos(b)+c) ==> "(-c - sin(a))/cos(b)"

// (x^2-1)/(x+1) = 0 --> x =
solve x ((x**2-1)/(x+1)) ==> "1"
Expand Down