【问题标题】:How can I translate this program from Vala to Genie?我怎样才能把这个程序从 Vala 翻译成 Genie?
【发布时间】:2016-07-13 15:53:21
【问题描述】:

我想引用了一些示例,但是我的 Google-fu 失败了,Genie 的文档页面也失败了。

我们举一个非常具体的例子,来自there的一些Vala代码:

using GLib;
using Gsl;

public class Test : GLib.Object
{
        public static void main (string[] args)
        {
                double[] a_data = new double[] { 0.18, 0.60, 0.57, 0.96,
                                                 0.41, 0.24, 0.99, 0.58,
                                                 0.14, 0.30, 0.97, 0.66,
                                                 0.51, 0.13, 0.19, 0.85 };

                double[] b_data = new double[] { 1.0, 2.0, 3.0, 4.0 };

                MatrixView m = MatrixView.array (a_data, 4, 4);
                VectorView b = VectorView.array (b_data);

                Vector x = new Vector (4);

                int s;

                Permutation p = new Permutation (4);

                LinAlg.LU_decomp ((Matrix)(&m.matrix), p, out s);
                LinAlg.LU_solve ((Matrix)(&m.matrix), p, (Vector)(&b.vector), x);

                stdout.printf("x = \n");
                Vector.fprintf(stdout, x, "%g");
        }
}

这是我尝试将该程序转换为 Genie 的方法:

[indent=4]

uses Gsl

init
    a_data: array of double = { 0.18, 0.60, 0.57, 0.96,
                                0.41, 0.24, 0.99, 0.58,
                                0.14, 0.30, 0.97, 0.66,
                                0.51, 0.13, 0.19, 0.85 }
    b_data: array of double = { 1.0,  2.0,  3.0,  4.0 }

    var m = Gsl.MatrixView.array(a_data, 4, 4)
    var b = Gsl.VectorView.array(b_data)

    var x = new Gsl.Vector(4)

    s:int = 0

    var p = new Gsl.Permutation(4)

    Gsl.LinAlg.LU_decomp(m, p, out s)
    Gsl.LinAlg.LU_solve(m, p, b, x)

    print "x =\n%g", x

根据this reference,这应该是正确的。

但它失败了:

LA.gs:12.28-12.32: error: syntax error, expected identifier
    var m = Gsl.MatrixView.array(a_data, 4, 4)
                           ^^^^^
LA.gs:13.28-13.32: error: syntax error, expected identifier
    var b = Gsl.VectorView.array(b_data)
                           ^^^^^
Compilation failed: 2 error(s), 0 warning(s)

那么,任何人都可以向我解释这个特定错误的含义吗? Genie 上下文中的标识符是什么?

在 Genie 中调用这种静态方法的正确方法是什么?

【问题讨论】:

    标签: vala genie


    【解决方案1】:

    数组是保留关键字,所以必须在保留关键字前面加上@-符号:

    var m = Gsl.MatrixView.@array(a_data, 4, 4)
    var b = Gsl.VectorView.@array(b_data)
    

    打印行也不正确,我已经修复了大部分问题:

    [indent=4]
    
    uses Gsl
    
    init
        a_data: array of double = { 0.18, 0.60, 0.57, 0.96,
                                    0.41, 0.24, 0.99, 0.58,
                                    0.14, 0.30, 0.97, 0.66,
                                    0.51, 0.13, 0.19, 0.85 }
        b_data: array of double = { 1.0,  2.0,  3.0,  4.0 }
    
        var m = Gsl.MatrixView.@array(a_data, 4, 4)
        var b = Gsl.VectorView.@array(b_data)
    
        var x = new Gsl.Vector(4)
    
        s:int = 0
    
        var p = new Gsl.Permutation(4)
    
        Gsl.LinAlg.LU_decomp((Matrix) m, p, out s)
        Gsl.LinAlg.LU_solve((Matrix) m, p, (Vector) b.vector, x)
    
        stdout.printf("x =\n")
        Vector.fprintf(stdout, x, "%g")
    

    这仍然无法编译,因为LU_ 方法需要一个我不能 100% 确定该怎么做的类型转换。

    更新:这段代码可以编译并运行,但我不知道它是否真的正确:

    [indent=4]
    
    uses Gsl
    
    init
        a_data: array of double = { 0.18, 0.60, 0.57, 0.96,
                                    0.41, 0.24, 0.99, 0.58,
                                    0.14, 0.30, 0.97, 0.66,
                                    0.51, 0.13, 0.19, 0.85 }
        b_data: array of double = { 1.0,  2.0,  3.0,  4.0 }
    
        var m = Gsl.MatrixView.@array(a_data, 4, 4)
        var b = Gsl.VectorView.@array(b_data)
    
        var x = new Gsl.Vector(4)
    
        s:int = 0
    
        var p = new Gsl.Permutation(4)
    
        mp : MatrixView* = &m
        vp : Vector** = &(b.vector)
    
        Gsl.LinAlg.LU_decomp((Matrix) mp, p, out s)
        Gsl.LinAlg.LU_solve((Matrix) mp, p, (Vector) vp, x)
    
        stdout.printf("x =\n")
        Vector.fprintf(stdout, x, "%g")
    

    【讨论】:

    • 所以,首先:谢谢! 这实际上按预期工作。我对程序的最后一部分完全没有希望:我打算稍后修复它,但你也做了。再次感谢!现在,我可以在工作基础上随心所欲地调整事物。最后一件事:保留关键字冲突是否无法解决?我的意思是:如果array 是单独的,如果它崩溃了也没关系:毕竟它是保留的。但是在这里,数组不可能是别的东西。那么它被认为是一个问题还是预期的东西?
    • 嗯...事实证明,您实际上不必从MatrixViewVector 中获取指针:可以直接使用MatrixView.matrixVector.vector 字段作为它是在 Vala 完成的(更好)。但不管怎样,你真的解开了局面!精灵真的很有趣:)
    • 我不是编译器开发人员,所以我不知道。如果你真的想知道,可以向bugzilla.gnome.org(产品 Vala,组件 Genie)提交错误报告。你当然可以随时自己阅读编译器的源代码并提出补丁;)。
    猜你喜欢
    • 2018-03-26
    • 1970-01-01
    • 2020-09-16
    • 2020-06-23
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多