How to communicate matrices et al between Julia and Reduce using Reduce.jl

One way forward for me is to combine the power of Reduce-algebra with Julia numerics. The Reduce.jl package should enable this. A crucial first step is to be able to communicate matrices of numbers between Julia and Reduce: more specifically, Reduce generates a matrix from its algebra, I communicate it to Julia, Julia computes eigenvalues and eigenvectors and a LU decomposition of a borderd-matrix, or QR, and then communicates these matrices back to Reduce-algebra for use in further algebra. But I just cannot see how this communication is actually accomplished. Please advise.

It would be helpful if you could provide an example script that fails.

Unfortunately, I really do not know how to start.
I can see the examples Reduce,jl, but almost never do the examples make it clear what of the printed information is ‘known’ to Julia and what to Reduce. Effectively I have no idea. :frowning:

Well, we have a REPL. Just try it out. You can also write some pseudo code.

If you create an issue at Reduce.jl you reach the author of the package, but I doubt that he can help you without a more concrete description of what you are trying to achieve.

OK will go to Reduce.jl However, I have figured out how to use function calls to do something—but something appears weird to me. I wonder if it is common to other “call” scenarios so will share here also.

I successfully do the following step-by-step by hand in the CLI (aka REPL I guess).

using Reduce
@force using Reduce.Algebra
println("starting reduce")
R"in \"randmat.txt\"$"   # executes Reduce commands in randa.txt
A = eval(rcall(:(randmat(6))))

The dialogue is

julia> using Reduce
julia> @force using Reduce.Algebra
julia> println("starting reduce")
starting reduce
julia> R"in \"randmat.txt\"$"   # executes Reduce commands in randmat.txt
julia> println("starting rcall of the Reduce function")
starting rcall of the Reduce function
julia> A = eval(rcall(:(randmat(6))))
6×6 Matrix{Int64}:
 0  0  0  0  1  6
 2  0  0  1  6  5
 2  0  0  3  7  6
 0  1  1  2  0  7
 3  0  0  6  7  3
 3  2  5  1  3  8
julia> println("finished rcall")
finished rcall
julia> 

But when I try to execute it a a sequence of commands in a file, it fails in the eval(rcall(.)) step: execution hangs waiting for something. So I expanded the eval(rcall()) and the problem appears to rcall (which I understand is derived from another “call()” function). Trace printing in the script reports the following, and hangs in the rcall

julia> include("test.jl")
starting reduce
setting up Reduce function randmat
doing rcall

Any suggestions?

You did not share the content of the file randmat.txt, so I cannot reproduce the issue.

Additional remark: Please read: GitHub - chakravala/Reduce.jl: Symbolic parser for Julia language term rewriting using REDUCE algebra

For loading Reduce packages you should use the command:

load_package(:scope)

where :scope is the name of the package to load.

Furthermore it is not clear to me why you use @eval, that should not be needed.

Hi, let me address each comment.
First, the content source of “randmat.txt” for Reduce is simply

matrixproc randmat(n); begin
    matrix a(n,n);
    for i:=1:n do for j:=1:n do a(i,j):=random(i+j);
    return a;
end$  
end;

Second, on load_package: why do you consider that loading Reduce packages has anything to do with the problem? load_package() just extends Reduce’s capabilities, I do not see how it can contribute to how Julia communicates with Reduce. I can see nothing in the Reduce.jl documentation saying there is an accompanying package for Reduce to load.

Third, on using eval. The rcall() returns an object of type Expr. For example “rcall(:(randmat(4)))” returns the answer “:([1 2 1 3; 1 0 3 4; 2 4 1 3; 3 0 5 6])” and then “eval(ans)” gives a Julia matrix, e.g.,

julia> rcall(:(randmat(4)))
:([1 0 3 1; 1 3 4 3; 0 4 3 6; 0 3 0 4])
julia> eval(ans)
4×4 Matrix{Int64}:
 1  0  3  1
 1  3  4  3
 0  4  3  6
 0  3  0  4

So, how can I not need eval()?

BTW further information: executing in Mac OSX 15.6 terminal window via the command “julia < test.jl” works; but executing “julia test.jl” fails due to the rcall() hanging. Completely mysterious to me.

I see, you created an issue: Reduce.jl/issues/62 . Good.

I will try to reproduce the issue on Linux tomorrow. If I should not be able to reproduce it, it might be a mac specific issue.