📄 matrix.java
字号:
public static void assertCongruent(float[] v1, float[] v2) { zliberror._assert(v1.length == v2.length, "vector length mismatch"); } //assertCongruent /** @deprecated moved to zlib.array */ public static void assertCongruent(double[] v1, double[] v2) { zliberror._assert(v1.length == v2.length, "vector length mismatch"); } //assertCongruent /** @deprecated moved to zlib.array */ public static void assertCongruent(int[][] m1, int[][] m2) { zliberror._assert((m1.length == m2.length) && (m1[0].length == m2[0].length), "matrix dimension mismatch"); } //assertCongruent /** @deprecated moved to zlib.array */ public static void assertCongruent(float[][] m1, float[][] m2) { zliberror._assert((m1.length == m2.length) && (m1[0].length == m2[0].length), "matrix dimension mismatch"); } //assertCongruent /** @deprecated moved to zlib.array */ public static void assertCongruent(double[][] m1, double[][] m2) { zliberror._assert((m1.length == m2.length) && (m1[0].length == m2[0].length), "matrix dimension mismatch"); } //assertCongruent /** @deprecated moved to zlib.array */ public static void assertDim(int[][] m, int nr, int nc) { zliberror._assert((m.length == nr) && (m[0].length == nc)); } //assertDim /** @deprecated moved to zlib.array */ public static void assertDim(float[][] m, int nr, int nc) { zliberror._assert((m.length == nr) && (m[0].length == nc)); } //assertDim /** @deprecated moved to zlib.array */ public static void assertDim(double[][] m, int nr, int nc) { zliberror._assert((m.length == nr) && (m[0].length == nc)); } //assertDim /** @deprecated moved to zlib.array */ public static void assertDim(int[] m, int nr) { zliberror._assert(m.length == nr); } //assertDim /** @deprecated moved to zlib.array */ public static void assertDim(float[] m, int nr) { zliberror._assert(m.length == nr); } //assertDim /** @deprecated moved to zlib.array */ public static void assertDim(double[] m, int nr) { zliberror._assert(m.length == nr); } //assertDim //---------------------------------------------------------------- /** * data is an array of n-dimensional data, not a matrix * depending on how you look at it. * Get the min/max bounds for each dimension. * Could be called column bounds. */ public static void getNDBounds(double[][] data, double[][] bounds) { int nd = data[0].length; zliberror._assert(nd == bounds.length, "matrix.getNDbounds:1"); zliberror._assert(bounds[0].length == 2, "matrix.getNDbounds:2"); for( int id=0; id < nd; id++ ) { bounds[id][0] = Double.POSITIVE_INFINITY; bounds[id][1] = Double.NEGATIVE_INFINITY; } int ndata = data.length; for( int i=0; i < ndata; i++ ) { for( int id=0; id < nd; id++ ) { double v = data[i][id]; if (v < bounds[id][0]) bounds[id][0] = v; if (v > bounds[id][1]) bounds[id][1] = v; } } } //getBounds //---------------------------------------------------------------- // clone //---------------------------------------------------------------- /** @deprecated moved to zlib.array */ public static double[][] clone(double[][] m) { double[][] mc = (double[][])m.clone(); for( int r=0; r < mc.length; r++ ) { mc[r] = (double[])m[r].clone(); } return mc; } //clone /** * return a double[][] copy of this float matrix * @deprecated moved to zlib.array */ public static double[][] doubleClone(float[][] matrix) { int nr = matrix.length; int nc = matrix[0].length; double[][] cmatrix = new double[nr][nc]; for( int r = 0; r < nr; r++ ) { for( int c = 0; c < nc; c++ ) { cmatrix[r][c] = matrix[r][c]; } } return cmatrix; } //doubleClone //---------------------------------------------------------------- /** * return a double[] copy of this float vector * @deprecated moved to zlib.array */ public static double[] doubleClone(float[] vec) { int nr = vec.length; double[] cvec = new double[nr]; for( int r = 0; r < nr; r++ ) { cvec[r] = vec[r]; } return cvec; } //doubleClone //---------------------------------------------------------------- /** * return a float[][] copy of this double matrix * @deprecated moved to zlib.array */ public static float[][] floatClone(double[][] matrix) { int nr = matrix.length; int nc = matrix[0].length; float[][] cmatrix = new float[nr][nc]; for( int r = 0; r < nr; r++ ) { for( int c = 0; c < nc; c++ ) { cmatrix[r][c] = (float)matrix[r][c]; } } return cmatrix; } //floatClone //---------------------------------------------------------------- /** * return a float[] copy of this double vector * @deprecated moved to zlib.array */ public static float[] floatClone(double[] vec) { int nr = vec.length; float[] cvec = new float[nr]; for( int r = 0; r < nr; r++ ) { cvec[r] = (float)vec[r]; } return cvec; } //floatClone //---------------------------------------------------------------- //---------------------------------------------------------------- // actual matrix operations //---------------------------------------------------------------- //---------------------------------------------------------------- //---------------------------------------------------------------- // zero //---------------------------------------------------------------- /** */ public static void zero(float[][] mat) { int nr = mat.length; int nc = mat[0].length; for( int r = 0; r < nr; r++ ) { float[] mr = mat[r]; for( int c = 0; c < nc; c++ ) { mr[c] = 0.f; } } } //zero /** */ public static void zero(double[][] mat) { int nr = mat.length; int nc = mat[0].length; for( int r = 0; r < nr; r++ ) { double[] mr = mat[r]; for( int c = 0; c < nc; c++ ) { mr[c] = 0.; } } } //zero //---------------------------------------------------------------- // colonEx, colonSet - matlab : operator //---------------------------------------------------------------- /** * colonExtract, like the matlab : operator, but only works with 2d arrays * colonex(0,0) = matlab mat(:,1) result is [nr,1] * colonex(0,1) = matlab mat(:,2) * colonex(1,0) = matlab mat(1,:) * colonex(1,1) = matlab mat(2,:) result is [1,nc] */ public static int[][] colonEx(int[][] mat, int dim, int el) { int nr = mat.length; int nc = mat[0].length; int[] size = new int[]{ nr,nc }; int[][] v = null; // dim=0 if (dim == 0) { int vdim = size[dim]; v = new int[vdim][1]; for( int i = 0; i < vdim; i++ ) { v[i][0] = mat[i][el]; } } else if (dim == 1) { int vdim = size[dim]; v = new int[1][vdim]; for( int i = 0; i < vdim; i++ ) { v[0][i] = mat[el][i]; } } else zliberror._assert(false, "colonEx only handles 2d arrays"); return v; } //colonEx /** * colonSet(m,0,k,v) = matlab mat(:,k+1) = v * colonSet(m,1,k,v) = matlab mat(k+1,:) = v */ public static void colonSet(int[][] mat, int dim, int el, int[][] v) { int nr = mat.length; int nc = mat[0].length; int[] size = new int[]{ nr,nc }; // dim=0 if (dim == 0) { int vdim = size[dim]; array.assertDim(v, vdim,1); for( int i = 0; i < vdim; i++ ) { mat[i][el] = v[i][0]; } } else if (dim == 1) { int vdim = size[dim]; array.assertDim(v, 1,vdim); for( int i = 0; i < vdim; i++ ) { mat[el][i] = v[0][i]; } } else zliberror._assert(false, "extract only handles 2d arrays"); } //colonSet //---------------------------------------------------------------- // identity //---------------------------------------------------------------- /** * float version */ public static void setIdentity(float[][] mat) { int nr = mat.length; int nc = mat[0].length; for( int r = 0; r < nr; r++ ) { for( int c = 0; c < nc; c++ ) { if (r == c) mat[r][c] = 1.f; else mat[r][c] = 0.f; } } } //setIdentity /** */ public static void setIdentity(double[][] mat) { int nr = mat.length; int nc = mat[0].length; for( int r = 0; r < nr; r++ ) { for( int c = 0; c < nc; c++ ) { if (r == c) mat[r][c] = 1.; else mat[r][c] = 0.; } } } //setIdentity //---------------------------------------------------------------- // min,max //---------------------------------------------------------------- /** */ public static int[][] min(int[][] m1, int[][] m2) { int nr = m1.length; int nc = m1[0].length; int[][] mr = new int[nr][nc]; min( m1, m2, mr ); return mr; } //min(int) /** */ public static void min(int[][] m1, int[][] m2, int[][] mr) { array.assertCongruent(m1, m2); int nr = m1.length; int nc = m1[0].length; for( int ir = 0; ir < nr; ir++ ) { int[] m1row = m1[ir]; int[] m2row = m2[ir]; int[] mrrow = mr[ir]; for( int ic = 0; ic < nc; ic++ ) { int m1v = m1row[ic]; int m2v = m2row[ic]; mrrow[ic] = (m1v < m2v) ? m1v : m2v; } } } //min(int) /** */ public static int[][] max(int[][] m1, int[][] m2) { int nr = m1.length; int nc = m1[0].length; int[][] mr = new int[nr][nc]; max( m1, m2, mr ); return mr; } //add(int) /** */ public static void max(int[][] m1, int[][] m2, int[][] mr) { array.assertCongruent(m1, m2); int nr = m1.length; int nc = m1[0].length; for( int ir = 0; ir < nr; ir++ ) { int[] m1row = m1[ir]; int[] m2row = m2[ir]; int[] mrrow = mr[ir]; for( int ic = 0; ic < nc; ic++ ) { int m1v = m1row[ic]; int m2v = m2row[ic]; mrrow[ic] = (m1v > m2v) ? m1v : m2v; } } } //max(int) //---------------------------------------------------------------- // scale //---------------------------------------------------------------- /** * functional */ public static float[] scalefun(float scale, float[] v) { int len = v.length; float[] sv = new float[len]; for( int i = 0; i < len; i++ ) { sv[i] = v[i] * scale; } return sv; } //scale public static void scale(float scale, float[] v) { int len = v.length; for( int i = 0; i < len; i++ ) { v[i] *= scale; } } //scale /** * functional */ public static double[] scalefun(double scale, double[] v) { int len = v.length; double[] sv = new double[len]; for( int i = 0; i < len; i++ ) { sv[i] = v[i] * scale; } return sv; } //scale public static void scale(double scale, double[] v) { int len = v.length; for( int i = 0; i < len; i++ ) { v[i] *= scale; } } //scale //---------------------------------------------------------------- // add //---------------------------------------------------------------- /** * functional */ public static int[][] addfun(int[][] m1, int[][] m2) { int nr = m1.length; int nc = m1[0].length; int[][] mr = new int[nr][nc]; add( m1, m2, mr ); return mr; } //add(int) /** */ public static void add(int[][] m1, int[][] m2, int[][] mr) { array.assertCongruent(m1, m2); array.assertCongruent(m1, mr); int nr = m1.length; int nc = m1[0].length; for( int ir = 0; ir < nr; ir++ ) { int[] m1row = m1[ir]; int[] m2row = m2[ir]; int[] mrrow = mr[ir]; for( int ic = 0; ic < nc; ic++ ) { mrrow[ic] = m1row[ic] + m2row[ic]; } } } //add(int) /** * mr = s1*v1 + s2*v2 */ public static void addscaled(float s1, float[] v1, float s2, float[] v2, float[] vr) { array.assertCongruent(v1, v2); array.assertCongruent(v1, vr); int len = v1.length; for( int i = 0; i < len; i++ ) { vr[i] = s1*v1[i] + s2*v2[i]; } } //addscaled(float) /** * mr = s1*m1 + s2*m2 */ public static void addscaled(float s1, float[][] m1, float s2, float[][] m2, float[][] mr) { array.assertCongruent(m1, m2); array.assertCongruent(m1, mr); int nr = m1.length; int nc = m1[0].length; for( int ir = 0; ir < nr; ir++ ) { float[] m1row = m1[ir]; float[] m2row = m2[ir]; float[] mrrow = mr[ir]; for( int ic = 0; ic < nc; ic++ ) { mrrow[ic] = s1 * m1row[ic] + s2 * m2row[ic]; } } } //addscaled(float) /** * mr = s1*m1 + s2*m2 */ public static void addscaled(double s1, double[][] m1, double s2, double[][] m2, double[][] mr) { array.assertCongruent(m1, m2); array.assertCongruent(m1, mr); int nr = m1.length; int nc = m1[0].length; for( int ir = 0; ir < nr; ir++ ) { double[] m1row = m1[ir]; double[] m2row = m2[ir]; double[] mrrow = mr[ir]; for( int ic = 0; ic < nc; ic++ ) { mrrow[ic] = s1 * m1row[ic] + s2 * m2row[ic]; } } } //addscaled(double) //---------------------------------------------------------------- /** */ public static float[][] addfun(float[][] m1, float[][] m2) { int nr = m1.length; int nc = m1[0].length; float[][] mr = new float[nr][nc]; add( m1, m2, mr ); return mr; } //add(float) public static void add(float[][] m1, float[][] m2, float[][] mr) { array.assertCongruent(m1, m2); array.assertCongruent(m1, mr); int nr = m1.length; int nc = m1[0].length; for( int ir = 0; ir < nr; ir++ ) { float[] m1row = m1[ir]; float[] m2row = m2[ir]; float[] mrrow = mr[ir]; for( int ic = 0; ic < nc; ic++ ) { mrrow[ic] = m1row[ic] + m2row[ic]; } } } //add(float) //---------------------------------------------------------------- /** */ public static double[][] addfun(double[][] m1, double[][] m2) { int nr = m1.length; int nc = m1[0].length; double[][] mr = new double[nr][nc];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -