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

Got "ERROR: LoadError: AssertionError: islinear" when using symbolic_linear_solve #1401

Open
ysaereve opened this issue Jan 13, 2025 · 1 comment

Comments

@ysaereve
Copy link

Issue

I've run into a bit of a puzzle with symbolic_linear_solve, and I was hoping you could lend your expertise. I'm working with two sets of linear equations (included below), and while one solves without a hitch, the other is giving me some trouble. I've been trying to understand why using Symbolics.linear_expansion, and it seems that Test Case 1 is returning true, while Test Case 2 is returning false. I'm a bit stuck, so any advice or suggestions you might have would be fantastic!

Test Case 1: (Works)

eq = 8.85 - 8.85 * mp ~ (1+0.13) * er

Test Case 2: (Got "ERROR: LoadError: AssertionError: islinear" error)

eq = 8.85 ~ er / (1-mp) * (1+0.13)

Code

#!/usr/bin/env -S julia +release
using Symbolics
using Pkg

function printpackageversion(names::Vector{String})
    deps = Pkg.dependencies()
    for (uuid, dep) in deps
        if dep.name in names
            println("$(dep.name): v$(dep.version)")
        end
    end
end

println("Julia: v$VERSION")
printpackageversion(["Symbolics"])

# er: exchange rate
# mp: margin percent
@variables er mp

# Test Case #1
begin
    println("\n# Test Case #1")
    @show eq = 8.85 - 8.85 * mp ~ (1+0.13) * er 
    @show Symbolics.linear_expansion(eq, mp)

    @show n = symbolic_linear_solve(eq, er, simplify = true)
    @show substitute(n, Dict(mp => 0.05))
    @show n = symbolic_linear_solve(eq, mp, simplify = true)
    @show substitute(n, Dict(er => 7.75))
end

# Test Case #2
begin
    println("\n# Test Case #2")
    @show eq = 8.85 ~ er / (1-mp) * (1+0.13) 
    @show Symbolics.linear_expansion(eq, mp)

    @show n = symbolic_linear_solve(eq, er, simplify = true)
    @show substitute(n, Dict(mp => 0.05))
    @show n = symbolic_linear_solve(eq, mp, simplify = true)
    @show substitute(n, Dict(er => 7.75))
end

Error

Julia: v1.11.2
Symbolics: v6.22.1

# Test Case #1
eq = (8.85 - 8.85mp ~ (1 + 0.13) * er) = 8.85 - 8.85mp ~ 1.13er
Symbolics.linear_expansion(eq, mp) = (8.85, -8.85 + 1.13er, true)
n = symbolic_linear_solve(eq, er, simplify = true) = 0.8849557522123894(8.85 - 8.85mp)
substitute(n, Dict(mp => 0.05)) = 7.4402654867256635
n = symbolic_linear_solve(eq, mp, simplify = true) = 0.11299435028248588(8.85 - 1.13er)
substitute(n, Dict(er => 7.75)) = 0.010451977401130071

# Test Case #2
eq = (8.85 ~ (er / (1 - mp)) * (1 + 0.13)) = 8.85 ~ (1.13er) / (1 - mp)
Symbolics.linear_expansion(eq, mp) = (0, 0, false)
n = symbolic_linear_solve(eq, er, simplify = true) = 7.831858407079646(1 - mp)
substitute(n, Dict(mp => 0.05)) = 7.4402654867256635
ERROR: LoadError: AssertionError: islinear
Stacktrace:
  [1] symbolic_linear_solve(eq::Equation, var::Num; simplify::Bool, check::Bool)
    @ Symbolics ~/.julia/packages/Symbolics/PxO3a/src/linear_algebra.jl:105
  [2] macro expansion
    @ show.jl:1232 [inlined]
  [3] top-level scope
    @ ~/Projects/Babel/Julia/TDSymbolics/TDSymbolics-TDislinear.jl:41
  [4] include(fname::String)
    @ Main ./sysimg.jl:38
  [5] run(debug_session::VSCodeDebugger.DebugAdapter.DebugSession, error_handler::VSCodeDebugger.var"#3#4"{String})
    @ VSCodeDebugger.DebugAdapter ~/.vscode-insiders/extensions/julialang.language-julia-1.127.2/scripts/packages/DebugAdapter/src/packagedef.jl:122
  [6] startdebugger()
    @ VSCodeDebugger ~/.vscode-insiders/extensions/julialang.language-julia-1.127.2/scripts/packages/VSCodeDebugger/src/VSCodeDebugger.jl:45
  [7] top-level scope
    @ ~/.vscode-insiders/extensions/julialang.language-julia-1.127.2/scripts/debugger/run_debugger.jl:12
  [8] include(mod::Module, _path::String)
    @ Base ./Base.jl:557
  [9] exec_options(opts::Base.JLOptions)
    @ Base ./client.jl:323
 [10] _start()
    @ Base ./client.jl:531
in expression starting at ~/Projects/Babel/Julia/TDSymbolics/TDSymbolics-TDislinear.jl:34
 *  Terminal will be reused by tasks, press any key to close it. 
@ChrisRackauckas
Copy link
Member

Case 2 isn't a linear expression. You can pre-process to multiply by 1-mp to make it one, but currently this solver function is not capable of doing that.

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

2 participants