首页 > 解决方案 > Julia UndefVarError 关于元编程

问题描述

我正在尝试为方程做一个求解器。当我运行代码时,X变量似乎是未定义的,但它打印出来很完美。我错过了什么?我应该给程序一些数字,而不是宏的操作,它应该创建应用操作的外积矩阵。

function msu()
    print("Insert how many values: ")
    quantity = parse(Int64, readline())
    values = []
    for i in 1:quantity
        println("x$i")
        num1 = parse(Float64, readline())
        push!(values, num1)
    end

    println(values)

    print("How many operations? ")
    quantity = parse(Int64, readline())
    ops = []
    for i in 1:quantity
        push!(ops, Meta.parse(readline()))
    end

    mat = zeros((quantity, quantity))

    for i in 1:length(mat)
        sum = 0
        for j in 1:length(values)
            # here begins problems, the following prints are for debugging purpose
            print(length(values))
            func = Meta.parse("$(ops[convert(Int64, ceil(j / quantity))]) * $(ops[convert(Int64, j % quantity)])")
            print(func)
            x = values[j]
            println(x)
            sum += eval(func)
        end

        mat[i] = sum
    end

    println(mat)
end

msu()

原始代码是西班牙语,如果您发现任何拼写错误,可能是因为我跳过了翻译。

标签: julia

解决方案


推荐阅读