我怎样才能将这个程序从Vala翻译成Genie?

时间:2016-07-13 15:53:22

标签: vala genie

我想 引用了一些例子,但是我的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");
        }
}

以下是我尝试将该程序转换为精灵的方法:

[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
根据{{​​3}},

应该是正确的。

但它失败了:

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中调用这种静态方法的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

数组是保留关键字,因此您必须在保留关键字前面添加@ -sign:

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")
相关问题