Skip to content
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

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

Open
linuxguy123 opened this issue Oct 31, 2024 · 5 comments

Comments

@linuxguy123
Copy link

linuxguy123 commented Oct 31, 2024

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
Copy link
Member

@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
Copy link
Member

n0rbed commented Nov 18, 2024

@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
Copy link
Member

n0rbed commented Nov 18, 2024

also, @linuxguy123 , fyi:
image

@n0rbed
Copy link
Member

n0rbed commented Nov 18, 2024

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
Copy link
Contributor

sumiya11 commented Nov 18, 2024

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

function transendence_basis(sys, vars)

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants