From 9ee890e0f033dc760cd6d1bb4ff32f352a282194 Mon Sep 17 00:00:00 2001 From: n0rbed Date: Sat, 23 Mar 2024 23:39:51 +0200 Subject: [PATCH 1/5] Error until solve_for is generalized for non-linear functions --- src/linear_algebra.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/linear_algebra.jl b/src/linear_algebra.jl index f13bc8854..03f0196a5 100644 --- a/src/linear_algebra.jl +++ b/src/linear_algebra.jl @@ -98,6 +98,15 @@ function solve_for(eq, var; simplify=false, check=true) # scalar case # the cases. a, b, islinear = linear_expansion(eq, var) check && @assert islinear + + for eqᵢ in eq + islinear &= Symbolics.isaffine(eqᵢ.lhs-eqᵢ.rhs, var) + end + + if !islinear + throw("solve_for is currently unable to solve non-linear equations.") + end + islinear || return nothing # a * x + b = 0 if eq isa AbstractArray && var isa AbstractArray From ef3aa928d6f8ca2904e32d49060946de0c645037 Mon Sep 17 00:00:00 2001 From: n0rbed Date: Sun, 24 Mar 2024 20:33:44 +0200 Subject: [PATCH 2/5] fixed failed tests --- src/linear_algebra.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/linear_algebra.jl b/src/linear_algebra.jl index 03f0196a5..dab40412a 100644 --- a/src/linear_algebra.jl +++ b/src/linear_algebra.jl @@ -93,7 +93,7 @@ julia> Symbolics.solve_for([x + y ~ 0, x - y ~ 2], [x, y]) -1.0 ``` """ -function solve_for(eq, var; simplify=false, check=true) # scalar case +function solve_for(eq::AbstractArray, var::AbstractArray; simplify=false, check=true) # scalar case # simplify defaults for `false` as canonicalization should handle most of # the cases. a, b, islinear = linear_expansion(eq, var) @@ -123,6 +123,7 @@ function solve_for(eq, var; simplify=false, check=true) # scalar case end solve_for(eq::Equation, var::T; x...) where {T<:AbstractArray} = solve_for([eq],var, x...) solve_for(eq::T, var::Num; x...) where {T<:AbstractArray} = first(solve_for(eq,[var], x...)) +solve_for(eq::Equation, var::Num; x...) = (solve_for([eq],[var], x...)) function _solve(A::AbstractMatrix, b::AbstractArray, do_simplify) A = Num.(SymbolicUtils.quick_cancel.(A)) From 6df3e597de654eb379ee1f21cd89321fae86396f Mon Sep 17 00:00:00 2001 From: n0rbed Date: Mon, 25 Mar 2024 01:06:41 +0200 Subject: [PATCH 3/5] attempt #2 --- src/linear_algebra.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/linear_algebra.jl b/src/linear_algebra.jl index dab40412a..3070be4e7 100644 --- a/src/linear_algebra.jl +++ b/src/linear_algebra.jl @@ -93,14 +93,18 @@ julia> Symbolics.solve_for([x + y ~ 0, x - y ~ 2], [x, y]) -1.0 ``` """ -function solve_for(eq::AbstractArray, var::AbstractArray; simplify=false, check=true) # scalar case +function solve_for(eq, var; simplify=false, check=true) # scalar case # simplify defaults for `false` as canonicalization should handle most of # the cases. a, b, islinear = linear_expansion(eq, var) check && @assert islinear - for eqᵢ in eq - islinear &= Symbolics.isaffine(eqᵢ.lhs-eqᵢ.rhs, var) + if eq isa AbstractArray + for eqᵢ in eq + islinear &= Symbolics.isaffine(eqᵢ.lhs-eqᵢ.rhs, var) + end + else + islinear &= Symbolics.isaffine(eq.lhs-eq.rhs, [var]) end if !islinear @@ -123,7 +127,6 @@ function solve_for(eq::AbstractArray, var::AbstractArray; simplify=false, check= end solve_for(eq::Equation, var::T; x...) where {T<:AbstractArray} = solve_for([eq],var, x...) solve_for(eq::T, var::Num; x...) where {T<:AbstractArray} = first(solve_for(eq,[var], x...)) -solve_for(eq::Equation, var::Num; x...) = (solve_for([eq],[var], x...)) function _solve(A::AbstractMatrix, b::AbstractArray, do_simplify) A = Num.(SymbolicUtils.quick_cancel.(A)) From 2833cf6391aaff0f2de4695e05ebdca848393582 Mon Sep 17 00:00:00 2001 From: n0rbed Date: Wed, 27 Mar 2024 23:58:00 +0200 Subject: [PATCH 4/5] Descriptive error messages made some tests fail, so removed it --- src/linear_algebra.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/linear_algebra.jl b/src/linear_algebra.jl index 3070be4e7..ba020d69e 100644 --- a/src/linear_algebra.jl +++ b/src/linear_algebra.jl @@ -107,10 +107,6 @@ function solve_for(eq, var; simplify=false, check=true) # scalar case islinear &= Symbolics.isaffine(eq.lhs-eq.rhs, [var]) end - if !islinear - throw("solve_for is currently unable to solve non-linear equations.") - end - islinear || return nothing # a * x + b = 0 if eq isa AbstractArray && var isa AbstractArray From 4514152eee53fd571366e4b628ddf97b993327da Mon Sep 17 00:00:00 2001 From: n0rbed Date: Thu, 28 Mar 2024 13:52:37 +0200 Subject: [PATCH 5/5] isaffine errors for some differential equations, catched these errors in case they happen, so now the function relies on linear_expansion for these cases --- src/linear_algebra.jl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/linear_algebra.jl b/src/linear_algebra.jl index ba020d69e..43906f34c 100644 --- a/src/linear_algebra.jl +++ b/src/linear_algebra.jl @@ -101,13 +101,19 @@ function solve_for(eq, var; simplify=false, check=true) # scalar case if eq isa AbstractArray for eqᵢ in eq - islinear &= Symbolics.isaffine(eqᵢ.lhs-eqᵢ.rhs, var) + try + islinear &= Symbolics.isaffine(eqᵢ.lhs-eqᵢ.rhs, var) + catch e + end end else - islinear &= Symbolics.isaffine(eq.lhs-eq.rhs, [var]) + try + islinear &= Symbolics.isaffine(eq.lhs-eq.rhs, [var]) + catch e + end end - islinear || return nothing + if !islinear return nothing end # a * x + b = 0 if eq isa AbstractArray && var isa AbstractArray x = _solve(a, -b, simplify)