Skip to content

julia generation generation does not work for scalars  #76

@jarlebring

Description

@jarlebring

Currently the Julia code generator does not work for scalars.

julia> (graph,_)=graph_ps([1 2 3.0 33 4.4 10.0]);
julia> gen_code("/tmp/mycode.jl",graph);
julia> include("/tmp/mycode.jl")
dummy! (generic function with 1 method)
julia> dummy(0.3)
ERROR: MethodError: no method matching similar(::Float64, ::Type{Float64})
Closest candidates are:
  similar(::UpperHessenberg, ::Type{T}, ::Tuple{Vararg{Int64, N}}) where {T, N}
...
julia> # Wrap it as a matrix and it works
julia> dummy(reshape([0.3],1,1))
1×1 Matrix{Float64}:
 2.8209400000000002

There are lines like this in the generated code:

copyto!(A_copy, A);

which assumes that we can modify the contents of the variable A_copy, i.e., that it is mutable; e.g., Float64-numbers are immutable.

Observation: The generated MATLAB-code is very close to code that could be executed in Julia. One needs only to replace the comment sign, remove the I=eye and change the function definition to make it run. Seems close to efficient Julia code for scalars.

Alternatives:

  1. Make the generated Julia code with LangJulia also work for scalars / immutable types
  2. Generate the function twice e.g. signature dummy(::Number) in LangJulia
  3. Take the matlab-code generation code and make it more general e.g LangMatlabJulia where comment_sign etc is a parameter. + Make the LangMatlab a wrapper for LangMatlabJulia

I have my doubts that option 0 is doable, since we have mul!-calls etc based on memory managements. Option 2 seems to be better in terms of avoiding code-repetition in our package, but would make matlab code generation more involved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions