为什么inv()失败?

时间:2019-02-06 23:49:36

标签: julia

当我尝试在Julia文档(v1.0.3)中的示例上运行public class Quick3Way{ public static void sort(Comparable[] a) { StdRandom.shuffle(a); System.out.println(Arrays.toString(a)); sort(a, 0, a.length - 1); } public static void sort(Object[] a, Comparator c) { StdRandom.shuffle(a); sort(a, 0, a.length - 1, c); } public static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; // partition int i = lo; int lt = lo; int gt = hi; while (i <= gt){ if (less(a[i], a[lt])) exch(a, i++, lt++); else if (less(a[lt], a[i])) exch(a, i, gt--); else i++; } sort(a, lo, lt - 1); sort(a, gt + 1, hi); } public static void sort(Object[] a, int lo, int hi, Comparator c) { if (hi <= lo) return; // partition int i = lo; int lt = lo; int gt = hi; while (i <= gt){ if (less(a[i], a[lt], c)) exch(a, i++, lt++); else if (less(a[lt], a[i], c)) exch(a, i, gt--); else i++; } sort(a, lo, lt - 1, c); sort(a, gt + 1, hi, c); } // test client public static void main(String[] args) { Integer[] arraypart = { 1,1,2,2,3,4,5,6,710,30,0,50}; Integer[] arraysort = new Integer[arraypart.length]; System.arraycopy(arraypart, 0, arraysort, 0, arraypart.length); System.out.print("Original:\t"); for (int str : arraypart) System.out.print(str); System.out.println(); System.out.print("Full sort:\t"); Quick3Way.sort(arraysort); for (int str : arraysort) System.out.print(str); System.out.println(); } // private private static void exch(Comparable[] a, int i, int j) { System.out.println(Arrays.toString(a)); Comparable tmp = a[i]; a[i] = a[j]; a[j] = tmp; System.out.println(Arrays.toString(a)); } private static void exch(Object[] a, int i, int j) { Object tmp = a[i]; a[i] = a[j]; a[j] = tmp; } private static boolean less(Comparable a, Comparable b) { return a.compareTo(b) < 0; } private static boolean less(Object a, Object b, Comparator c) { return c.compare(a, b) < 0; } } 函数时,出现错误。代码如下(直接来自文档):

inv()

它可以与julia> M = [2 5; 1 3] 2×2 Array{Int64,2}: 2 5 1 3 julia> N = inv(M) ERROR: MethodError: objects of type Array{Float64,2} are not callable Use square brackets [] for indexing an Array. 一起使用,但是我得到一些非常小的浮点值。有什么想法为什么我无法让pinv()在这种极其简单的情况下工作?

1 个答案:

答案 0 :(得分:1)

错误消息表明您以前定义了一个名为inv的变量,它是一个浮点矩阵,然后尝试将此矩阵用作函数,例如

julia> inv = rand(2, 2);

julia> M = [2 5; 1 3];

julia> inv(M)
ERROR: MethodError: objects of type Array{Float64,2} are not callable
Use square brackets [] for indexing an Array.

您可以通过重新启动(从而清除inv的含义)或使用完全限定的名称来访问inv功能:

julia> import LinearAlgebra

julia> LinearAlgebra.inv(M)
2×2 Array{Float64,2}:
  3.0  -5.0
 -1.0   2.0