-
Notifications
You must be signed in to change notification settings - Fork 16
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
Simple Reduce Statement Fails to Compile Inside Module #32
Comments
That is incorrect, it is not a bug, you need to use an module DebugModule
using ForceImport
@force using Reduce.Algebra
using ReduceLinAlg
f(a,b) = (:x - a) * (:x - b)
g(a,b) = int( f(a,b), :x)
function __init__()
global g0
g0 = g(:a,:b) - sub( (:(x=b), ), g(:a,:b))
end
end When I load the module, it works: julia> DebugModule.g0
:(-((((3b - 2x) * x - 3 * (2b - x) * a) * x + (3a - b) * b ^ 2)) / 6) You can't use |
This is still odd to me since ultimately my use of g0 and what will follow are compile time calculations not run time calculations. My intention is to do some symbolic pre-processing and then compile functions derived from the result. However init() is primarily designated for run time initialization; but declaring g0 as a const or a global does not make the error go away, and I know that I can declare global variables or constants inside modules. So this error still is odd to me. Perhaps you can explain it further? What do you mean by it needs to be initialized first? How does that init command initialize Reduce? Is module compilation on it's own insufficient to do Reduce computation? Thanks in advance for your help. |
In that case, here is how to get the situation you want: module DebugModule
using Reduce
@force using Reduce.Algebra
using ReduceLinAlg
f(a,b) = (:x - a) * (:x - b)
g(a,b) = int( f(a,b), :x)
Reduce.Load()
g0 = g(:a,:b) - sub( (:(x=b), ), g(:a,:b))
end It is recommended to only use symbolic computation within your However, if you really, really want to, you can use |
It's still not compiling on my rig. Am I doing something wrong? I'm on linux mint 19.2, using Julia 1.2. Adding in the Reduce.Load() command (copied and pasted from your example) still gives me this error:
|
Alright, looks like module DebugModule
using Reduce
@force using Reduce.Algebra
using ReduceLinAlg
f(a,b) = (:x - a) * (:x - b)
g(a,b) = int( f(a,b), :x)
Reduce.Preload()
g0 = g(:a,:b) - sub( (:(x=b), ), g(:a,:b))
end This is available in the most recent commit of |
@chakravala Thanks so much for addressing this. Unfortunately when I tried to add the master branch of reduce, which contains the new function, I ran into some problems during the build. It kind of looks like I am missing an updated dependency which has the presumably linear algebra functions mateign and cofacter.
|
You are using |
Thanks for pointing that out. I was able to build it after restarting the REPL. The use case of doing a bunch of symbolic calculations and then using the results to generate julia functions is a pretty common theme, with several web search hits. However when you want the function to return an array there appears to be a bit of a gotcha. It's easy enough to generate an array of symbolic expressions say of the form You can do |
To quote arrays the way you describe, I've been doing this: julia> x = [:a , :b]
2-element Array{Symbol,1}:
:a
:b
julia> arr = :([ $(x...) ])
:([a, b])
julia>
|
Another way is like this julia> Expr(:vect,[:a,:b]...)
:([a, b]) Have a look at the output of julia> dump(:([a,b]))
Expr
head: Symbol vect
args: Array{Any}((2,))
1: Symbol a
2: Symbol b |
Thanks for the comments guys. I was beating my head against this wall for a while. |
Well here we are at Julia 1.5 and Reduce.Preload() is no longer allowed inside a module. I'm getting this kind of error:
The first 7 lines of the module are:
Is there any work-around for this? I really liked the feature of being able to precompile symbolic code into a module. Thanks once again. |
The problem is the definition of Prupose of offs = ""
for o in offlist
o != :nat && (offs = offs*"off $o; ")
end
write(rs.input,"off nat; $EOTstr;\n")
banner = readuntil(rs.output,EOT) |> String
readavailable(rs.output)
rcsl = occursin(" CSL ",banner)
if Sys.iswindows()
banner = replace(banner,r"\r" => "")
println(split(String(banner),'\n')[rcsl ? 1 : end-3])
else
ReduceCheck(banner)
println(split(String(banner),'\n')[rcsl ? 1 : end-3])
end
load_package(:rlfi)
offs |> RExpr |> rcall
rcall(R"on savestructr")
show(devnull,"text/latex",R"int(sinh(e**i*z),z)")
R"x" == R"x"
ListPrint(0) Work around is to duplicate the code for This should now work again on the latest commit. Let me know if you can think of a more elegant way to resolve the problem without literally duplicating the initialization code into the PS, please do share the projects you are working on, I would like to see your work. |
My "workaround" was going to be to create a script that ran Reduce and then render it's output as expression strings, which would then be written to another file, which would be included in a module. That approach is decidedly ugly, but it would certainly make all the module precompile errors go away. |
I added the new #master version of the package and Pkg.status() shows I'm at version 1.2.8. However I'm still seeing this error namely:
The first 7 lines of the module are as before. |
This time it is due to my usage of function ==(r::RExpr, s::RExpr)
n = expand(r).str
m = expand(s).str
l=length(n)
l≠length(m) && (return false)
b = true
for j∈1:l
b &= "if($(n[j]))=($(m[j]))then 1 else 0"|>rcall|>Meta.parse|>eval|>Bool
end
return b
end However, I don't have time to immediately work on a fix, or to focus on all the other potential problems with this. |
I understand. Thanks for your help so far. I will try to find another approach. I do feel like an important use case for symbolic processing is the programmatic generation of code, especially optimized code. I've frequently used maple in that capacity. |
If my work is so important, maybe i should be getting paid. This is a bonus feature. You don't even share your derived code with me. Why would i care about helping you with a proprietary code base and you don't pay me? |
Sorry it's not my call. I'm under NDA and do not have either the authority to release my code nor hire you. My particular application though is quite trivial. I'm simply using symbolic processing to create splines with various boundary conditions. I could have used any symbolic tool for this. I just happen to like the integration with Julia that your tool provides. I hope you do find a worthy sponsor for your efforts. Just FYI, the workaround of running your Reduce package as a preprocessor, then writing out the answers to a text file, worked like a charm. |
The following code snippet runs just fine in Julia 1.2, unless it's put into a module.
Now see what happens when I try to use this module.
If I copy and paste the code into the julia REPL it works fine and the module loads if the last line,
g0 = g(:a,:b) - sub( (:(x=b), ), g(:a,:b))
is commented out. This looks like a bug to me.The text was updated successfully, but these errors were encountered: