Skip to content

Commit 0f33d8c

Browse files
author
Tobias Weimer
committed
More efficient data copying
1 parent 2dee537 commit 0f33d8c

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/jama2/Matrix.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,19 @@ public static Matrix constructWithCopy(final double A[][])
8686
{
8787
final int m = A.length;
8888
final int n = A[0].length;
89-
final Matrix X = new Matrix(m, n);
90-
final double[][] C = X.getArray();
91-
for (int i = 0; i < m; i++)
89+
final double C[][] = new double[m][];
90+
for (int i = 0; i < C.length; i++)
9291
{
93-
if (A[i].length != n)
94-
{
95-
throw new IllegalArgumentException("All rows must have the same length."); //$NON-NLS-1$
96-
}
97-
for (int j = 0; j < n; j++)
98-
{
99-
C[i][j] = A[i][j];
100-
}
92+
if (A[i].length != n)
93+
{
94+
throw new IllegalArgumentException("All rows must have the same length."); //$NON-NLS-1$
95+
}
96+
else
97+
{
98+
C[i] = Arrays.copyOf(A[i], n);
99+
}
101100
}
102-
return X;
101+
return new Matrix(m, n, C);
103102
}
104103

105104
/**
@@ -705,10 +704,10 @@ public double[][] getArray()
705704
*/
706705
public double[][] getArrayCopy()
707706
{
708-
final double C[][] = new double[this.A.length][];
707+
final double C[][] = new double[this.m][];
709708
for (int i = 0; i < C.length; i++)
710709
{
711-
C[i] = Arrays.copyOf(this.A[i], this.A[i].length);
710+
C[i] = Arrays.copyOf(this.A[i], this.n);
712711
}
713712
return C;
714713
}

0 commit comments

Comments
 (0)