JuliaSymbolics / Symbolics.jl

Symbolic programming for the next generation of numerical software
https://docs.sciml.ai/Symbolics/stable/
Other
1.37k stars 153 forks source link

Symbolically solving a system of equations… “Matrix is not square” error #1336

Open linuxguy123 opened 2 weeks ago

linuxguy123 commented 2 weeks ago

I’m trying to find Requ(ivalent) for a circuit in terms of the 5 resistors in the circuit.

I’ve written out the equations for the circuit as follows:

using Symbolics
using Nemo
@variables i1 i2 i3 i4 i5 iIn;
equ1= iIn ~ i1 + i4
equ2 = iIn ~ i3 + i5
equ3 = i3 ~ i2 + i4
equ4 = i1 ~ i2 + i5
equ5 = i1 - i5 ~ i3 - i4
@variables R1 R2 R3 R4 R5 Requ
@variables Vin
equ6 = Vin ~ R1*i1 + R2*i2 + R3 * i3
equ7 = Vin ~ i4*R4 + i3*R3
equ8 = Vin ~ R1*i1 + R5 * i5
equ9 = Requ ~ Vin/iIn
equations = [equ1, equ2, equ3, equ4, equ5, equ6, equ7, equ8, equ9]
solution = Symbolics.solve_for(equations, Requ )

I get a “DimensionMismatch: matrix is not square: dimensions are (9, 1)” error.

Stack trace

Here is what happened, the most recent locations are first:

    checksquare @ LinearAlgebra.jl:302
    UpperTriangular @ triangular.jl:22
    UpperTriangular @ triangular.jl:28
    ldiv!(A::LinearAlgebra.LU{Symbolics.Num, Matrix{Symbolics.Num}, Vector{Int64}}, B::Vector{Symbolics.Num}) @ lu.jl:503
    ldiv(F::LinearAlgebra.LU{Symbolics.Num, Matrix{Symbolics.Num}, Vector{Int64}}, B::Vector{Symbolics.Num}) @ LinearAlgebra.jl:674
    \ @ LinearAlgebra.jl:649
    _solve(A::Matrix{Symbolics.Num}, b::Vector{Symbolics.Num}, do_simplify::Bool) @ linear_algebra.jl:127
    symbolic_linear_solve(eq::Vector{Symbolics.Equation}, var::Vector{Symbolics.Num}; simplify::Bool, check::Bool) @ linear_algebra.jl:109
    symbolic_linear_solve @ linear_algebra.jl:101
    symbolic_linear_solve(eq::Vector{Symbolics.Equation}, var::Symbolics.Num; x::@Kwargs{simplify::Bool, check::Bool}) @ linear_algebra.jl:121
    symbolic_linear_solve @ linear_algebra.jl:121
    #solve_for#389 @ linear_algebra.jl:72
    solve_for(eq::Vector{Symbolics.Equation}, var::Symbolics.Num) @ linear_algebra.jl:70
    This cell: line 1

    solution = Symbolics.solve_for(equations, Requ )

What am I doing wrong ?

ChrisRackauckas commented 1 week ago

@n0rbed do you have a idea for this one? I guess we only support square linear subsystems because non-square could only give the L2 solution? It might be good to throw a more informative error on that.

n0rbed commented 1 day ago

@ChrisRackauckas The author didnt use symbolic_solve here; If he did it wouldnt have worked aswel - We do support parameterizing in symbolic_solve but its a little weak right now (the system is 9 equations in 12 variables)

ERROR: DomainError with The monomial ordering is invalid.:

For symbolic_linear_solve, yes you're right

n0rbed commented 1 day ago

also, @linuxguy123 , fyi: image

n0rbed commented 23 hours ago

If we input

Requ*iIn ~ Vin

instead of

Requ ~ Vin/iIn

For eq9, we get:

1-element Vector{Any}:
 Dict{Num, Any}(R2 => R2, i2 => (R3*R5*Vin - R3*Requ*Vin + R4*R5*Vin - R5*Requ*Vin) / (R2*R3*Requ + R2*R4*Requ + R3*R4*Requ + R4*R5*Requ), R1 => (-R2*R3*R5 + R2*R3*Requ - R2*R4*R5 + R2*R4*Requ + R2*R5*Requ - R3*R4*R5 + R3*R4*Requ + R4*R5*Requ) / (R2*R3 + R2*R4 - R2*Requ + R3*R4 + R3*R5 - R3*Requ + R4*R5 - R5*Requ), R5 => R5, Requ => Requ, Vin => Vin, iIn => Vin / Requ, i1 => (R2*R3*Vin + R2*R4*Vin - R2*Requ*Vin + R3*R4*Vin + R3*R5*Vin - R3*Requ*Vin + R4*R5*Vin - R5*Requ*Vin) / (R2*R3*Requ + R2*R4*Requ + R3*R4*Requ + R4*R5*Requ), i4 => (R2*Requ*Vin - R3*R5*Vin + R3*Requ*Vin + R5*Requ*Vin) / (R2*R3*Requ + R2*R4*Requ + R3*R4*Requ + R4*R5*Requ), R4 => R4…)

So solve_multivar suffers a bit when fractions are present. It's a bit difficult to validate this because we have some correctness issues with transendence_basis and the output solely depends on that for nonsquare systems.

sumiya11 commented 23 hours ago

Right, non-square are supported, but with a caveat. In transcendence_basis, the heuristic choice is not correct for systems whose irreducible components have different transcendence bases

https://github.com/JuliaSymbolics/Symbolics.jl/blob/2bc3c5485962950bfc7653fcfd9b9ac0b92a5fd5/ext/SymbolicsGroebnerExt.jl#L297

I had plans to fix this when I had time; for now we can perhaps put a note in the docs that non-square solution is not proven.

Btw, do we get 0 if we substitute the solution back in this example?