|
|
|
@ -76,7 +76,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
{ |
|
|
|
if (Control.ParallelizeOperation(x.Length)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); |
|
|
|
CommonParallel.For(0, y.Length, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
result[i] = y[i] + x[i]; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -90,7 +96,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
{ |
|
|
|
if (Control.ParallelizeOperation(x.Length)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); |
|
|
|
CommonParallel.For(0, y.Length, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
result[i] = y[i] + (alpha*x[i]); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -128,7 +140,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
{ |
|
|
|
if (Control.ParallelizeOperation(x.Length)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); |
|
|
|
CommonParallel.For(0, x.Length, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
result[i] = alpha*x[i]; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -207,7 +225,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
|
|
|
|
if (Control.ParallelizeOperation(x.Length)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, y.Length, index => { result[index] = x[index] + y[index]; }); |
|
|
|
CommonParallel.For(0, y.Length, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
result[i] = x[i] + y[i]; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -252,7 +276,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
|
|
|
|
if (Control.ParallelizeOperation(x.Length)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, y.Length, index => { result[index] = x[index] - y[index]; }); |
|
|
|
CommonParallel.For(0, y.Length, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
result[i] = x[i] - y[i]; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -297,7 +327,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
|
|
|
|
if (Control.ParallelizeOperation(x.Length)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, y.Length, index => { result[index] = x[index] * y[index]; }); |
|
|
|
CommonParallel.For(0, y.Length, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
result[i] = x[i]*y[i]; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -342,7 +378,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
|
|
|
|
if (Control.ParallelizeOperation(x.Length)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, y.Length, index => { result[index] = x[index] / y[index]; }); |
|
|
|
CommonParallel.For(0, y.Length, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
result[i] = x[i]/y[i]; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -1395,17 +1437,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
throw new ArgumentException(Resources.ArgumentReferenceDifferent); |
|
|
|
} |
|
|
|
|
|
|
|
if (Control.ParallelizeOperation(columnsB * 10)) |
|
|
|
{ |
|
|
|
CommonParallel.For(0, columnsB, c => DoCholeskySolve(a, orderA, b, c)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
for (var index = 0; index < columnsB; index++) |
|
|
|
CommonParallel.For(0, columnsB, (u, v) => |
|
|
|
{ |
|
|
|
DoCholeskySolve(a, orderA, b, index); |
|
|
|
} |
|
|
|
} |
|
|
|
for (int i = u; i < v; i++) |
|
|
|
{ |
|
|
|
DoCholeskySolve(a, orderA, b, i); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -1553,7 +1591,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
CommonParallel.For(0, rowsR, i => q[(i * rowsR) + i] = Complex.One); |
|
|
|
CommonParallel.For(0, rowsR, (a, b) => |
|
|
|
{ |
|
|
|
for (int i = a; i < b; i++) |
|
|
|
{ |
|
|
|
q[(i*rowsR) + i] = Complex.One; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
var minmn = Math.Min(rowsR, columnsR); |
|
|
|
for (var i = 0; i < minmn; i++) |
|
|
|
@ -1762,14 +1806,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
var tmp = column * rowCount; |
|
|
|
var index = tmp + row; |
|
|
|
|
|
|
|
CommonParallel.For( |
|
|
|
row, |
|
|
|
rowCount, |
|
|
|
i => |
|
|
|
CommonParallel.For(row, rowCount, (u, v) => |
|
|
|
{ |
|
|
|
var iIndex = tmp + i; |
|
|
|
work[iIndex - row] = a[iIndex]; |
|
|
|
a[iIndex] = Complex.Zero; |
|
|
|
for (int i = u; i < v; i++) |
|
|
|
{ |
|
|
|
var iIndex = tmp + i; |
|
|
|
work[iIndex - row] = a[iIndex]; |
|
|
|
a[iIndex] = Complex.Zero; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
var norm = Complex.Zero; |
|
|
|
@ -1793,11 +1837,23 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
} |
|
|
|
|
|
|
|
a[index] = -norm; |
|
|
|
CommonParallel.For(0, rowCount - row, i => work[tmp + i] /= norm); |
|
|
|
CommonParallel.For(0, rowCount - row, (u, v) => |
|
|
|
{ |
|
|
|
for (int i = u; i < v; i++) |
|
|
|
{ |
|
|
|
work[tmp + i] /= norm; |
|
|
|
} |
|
|
|
}); |
|
|
|
work[tmp] += 1.0; |
|
|
|
|
|
|
|
var s = (1.0 / work[tmp]).SquareRoot(); |
|
|
|
CommonParallel.For(0, rowCount - row, i => work[tmp + i] = work[tmp + i].Conjugate() * s); |
|
|
|
CommonParallel.For(0, rowCount - row, (u, v) => |
|
|
|
{ |
|
|
|
for (int i = u; i < v; i++) |
|
|
|
{ |
|
|
|
work[tmp + i] = work[tmp + i].Conjugate()*s; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|
@ -1965,7 +2021,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
} |
|
|
|
|
|
|
|
int rowsQ, columnsQ, rowsR, columnsR; |
|
|
|
if( method == QRMethod.Full) |
|
|
|
if (method == QRMethod.Full) |
|
|
|
{ |
|
|
|
rowsQ = columnsQ = rowsR = rowsA; |
|
|
|
columnsR = columnsA; |
|
|
|
@ -1976,24 +2032,24 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
columnsQ = rowsR = columnsR = columnsA; |
|
|
|
} |
|
|
|
|
|
|
|
if (r.Length != rowsR * columnsR) |
|
|
|
if (r.Length != rowsR*columnsR) |
|
|
|
{ |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, rowsR * columnsR), "r"); |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, rowsR*columnsR), "r"); |
|
|
|
} |
|
|
|
|
|
|
|
if (q.Length != rowsQ * columnsQ) |
|
|
|
if (q.Length != rowsQ*columnsQ) |
|
|
|
{ |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, rowsQ * columnsQ), "q"); |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, rowsQ*columnsQ), "q"); |
|
|
|
} |
|
|
|
|
|
|
|
if (b.Length != rowsA * columnsB) |
|
|
|
if (b.Length != rowsA*columnsB) |
|
|
|
{ |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, rowsA * columnsB), "b"); |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, rowsA*columnsB), "b"); |
|
|
|
} |
|
|
|
|
|
|
|
if (x.Length != columnsA * columnsB) |
|
|
|
if (x.Length != columnsA*columnsB) |
|
|
|
{ |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, columnsA * columnsB), "x"); |
|
|
|
throw new ArgumentException(string.Format(Resources.ArgumentArrayWrongLength, columnsA*columnsB), "x"); |
|
|
|
} |
|
|
|
|
|
|
|
var sol = new Complex[b.Length]; |
|
|
|
@ -2005,56 +2061,62 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
var column = new Complex[rowsA]; |
|
|
|
for (var j = 0; j < columnsB; j++) |
|
|
|
{ |
|
|
|
var jm = j * rowsA; |
|
|
|
CommonParallel.For(0, rowsA, k => column[k] = sol[jm + k]); |
|
|
|
CommonParallel.For( |
|
|
|
0, |
|
|
|
columnsA, |
|
|
|
i => |
|
|
|
var jm = j*rowsA; |
|
|
|
CommonParallel.For(0, rowsA, (u, v) => |
|
|
|
{ |
|
|
|
var im = i * rowsA; |
|
|
|
|
|
|
|
var sum = Complex.Zero; |
|
|
|
for (var k = 0; k < rowsA; k++) |
|
|
|
for (int k = u; k < v; k++) |
|
|
|
{ |
|
|
|
sum += q[im + k].Conjugate() * column[k]; |
|
|
|
column[k] = sol[jm + k]; |
|
|
|
} |
|
|
|
}); |
|
|
|
CommonParallel.For(0, columnsA, (u, v) => |
|
|
|
{ |
|
|
|
for (int i = u; i < v; i++) |
|
|
|
{ |
|
|
|
var im = i*rowsA; |
|
|
|
|
|
|
|
sol[jm + i] = sum; |
|
|
|
var sum = Complex.Zero; |
|
|
|
for (var k = 0; k < rowsA; k++) |
|
|
|
{ |
|
|
|
sum += q[im + k].Conjugate()*column[k]; |
|
|
|
} |
|
|
|
|
|
|
|
sol[jm + i] = sum; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// Solve R*X = Y;
|
|
|
|
for (var k = columnsA - 1; k >= 0; k--) |
|
|
|
{ |
|
|
|
var km = k * rowsR; |
|
|
|
var km = k*rowsR; |
|
|
|
for (var j = 0; j < columnsB; j++) |
|
|
|
{ |
|
|
|
sol[(j * rowsA) + k] /= r[km + k]; |
|
|
|
sol[(j*rowsA) + k] /= r[km + k]; |
|
|
|
} |
|
|
|
|
|
|
|
for (var i = 0; i < k; i++) |
|
|
|
{ |
|
|
|
for (var j = 0; j < columnsB; j++) |
|
|
|
{ |
|
|
|
var jm = j * rowsA; |
|
|
|
sol[jm + i] -= sol[jm + k] * r[km + i]; |
|
|
|
var jm = j*rowsA; |
|
|
|
sol[jm + i] -= sol[jm + k]*r[km + i]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Fill result matrix
|
|
|
|
CommonParallel.For( |
|
|
|
0, |
|
|
|
columnsR, |
|
|
|
row => |
|
|
|
CommonParallel.For(0, columnsR, (u, v) => |
|
|
|
{ |
|
|
|
for (var col = 0; col < columnsB; col++) |
|
|
|
for (int row = u; row < v; row++) |
|
|
|
{ |
|
|
|
x[(col * columnsA) + row] = sol[row + (col * rowsA)]; |
|
|
|
for (var col = 0; col < columnsB; col++) |
|
|
|
{ |
|
|
|
x[(col*columnsA) + row] = sol[row + (col*rowsA)]; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Computes the singular value decomposition of A.
|
|
|
|
@ -2787,7 +2849,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra |
|
|
|
// Copy stemp to s with size adjustment. We are using ported copy of linpack's svd code and it uses
|
|
|
|
// a singular vector of length rows+1 when rows < columns. The last element is not used and needs to be removed.
|
|
|
|
// We should port lapack's svd routine to remove this problem.
|
|
|
|
CommonParallel.For(0, Math.Min(rowsA, columnsA), index => s[index] = stemp[index]); |
|
|
|
Array.Copy(stemp, s, Math.Min(rowsA, columnsA)); |
|
|
|
|
|
|
|
// On return the first element of the work array stores the min size of the work array could have been
|
|
|
|
// work[0] = Math.Max(3 * Math.Min(aRows, aColumns) + Math.Max(aRows, aColumns), 5 * Math.Min(aRows, aColumns));
|
|
|
|
|