1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
newPackage(
"NumericalLinearAlgebra",
Version => "1.16",
Date => "Dec 2020",
Authors => {
{Name => "Robert Krone",
Email => "krone@math.gatech.edu"},
{Name => "Marc Harkonen",
Email => "harkonen@gatech.edu"},
{Name => "Anton Leykin",
Email => "anton.leykin@gmail.com"}
},
Headline => "numerically compute local dual space and Hilbert functions",
Keywords => {"Numerical Linear Algebra"},
PackageExports => {"LLLBases"},
AuxiliaryFiles => false
)
export{
"Tolerance",
"Normalize",
"numericalKernel",
"numericalRank", "isFullNumericalRank",
"numericalImage",
"colReduce"
}
-- Default tolerance value respectively for exact fields and inexact fields
defaultT = R -> if precision 1_R == infinity then 0 else 1e-6;
getTolerance = true >> opts -> R -> if not opts.?Tolerance or opts.Tolerance === null then defaultT(R) else opts.Tolerance;
-- conjugate all entries of the matrix (should be a part of M2!!!)
conjugate Matrix := Matrix =>M -> matrix(entries M / (row->row/conjugate))
-- installPackage doesn't complain about the absence of conjugate
numericalKernel = method(Options => {Tolerance => null})
numericalKernel (Matrix) := Matrix => o -> M -> (
R := ring M;
tol := getTolerance(R,o);
(m,n) := (numrows M, numcols M);
if m == 0 then return id_(source M);
if n == 0 then return map(R^0,R^0,0);
(S,U,Vh) := SVD M;
cols := positions(S, sv->(sv > tol));
K := submatrix'(transpose Vh,,cols);
if K == 0 then K else conjugate K
)
numericalRank = method(Options=>{Threshold=>1e-4})
numericalRank Matrix := o -> M -> (
if not member(class ring M, {RealField,ComplexField})
then error "matrix with real or complex entries expected";
t := o.Threshold;
N := if t<=1 then M -- use t as an absolute cutoff for singular values, otherwise look for a "gap"
else matrix apply(entries M, row->( -- normalize "large" rows (a hack!!!)
m := max(row/abs);
if m<1 then row else apply(row,e->e/m)
));
S := first SVD N;
r := 0; last's := 1;
for i to #S-1 do (
if t>1 then (if t*S#i < last's
then break
else (r = r + 1; last's = S#i)
)
else (
if S#i>t then r = r + 1
else break
)
);
r
)
isFullNumericalRank = method(Options=>{Threshold=>1e-4})
isFullNumericalRank Matrix := o -> M -> (
r := numericalRank(M,o);
r == min(numColumns M, numRows M)
)
TEST ///
N = matrix {{0.001, 0, 0}, {1, 1, 3}, {2, 2, 5.999}}
assert(numericalRank(N,Threshold=>0.01) == 1)
assert not isFullNumericalRank(N,Threshold=>0.001)
assert isFullNumericalRank(N,Threshold=>0.00001)
///
TEST ///
N = matrix {{0.001, 0, 0}, {1, 1, 3}, {2, 2, 5.999}}
K = numericalKernel(N, Tolerance=>0.001)
assert(numcols K == 2)
assert(norm(N*K) < 0.001)
///
--performs Gaussian reduction on M
colReduce = method(Options => {Tolerance => null, Normalize => true, Reverse => false})
colReduce Matrix := o -> M -> (
if o.Reverse then M = matrix reverse(entries M);
tol := getTolerance(ring M,o);
if tol == 0 then M = gens gb M
else (
M = mutableMatrix sub(M, ultimate(coefficientRing, ring M));
(m,n) := (numrows M, numcols M);
j := 0; --column of pivot
for i in reverse(0..m-1) do (
if debugLevel >= 1 then <<i<<"/"<<m-1<<endl;
if j >= n then break;
a := j + maxPosition apply(j..n-1, l->(abs M_(i,l)));
c := M_(i,a);
if abs c <= tol then (for k from j to n-1 do M_(i,k) = 0; continue);
columnSwap(M,a,j);
if o.Normalize then (columnMult(M,j,1/c); c = 1);
for k from 0 to n-1 do if k != j then columnAdd(M,k,-M_(i,k)/c,j);
j = j+1;
);
M = (new Matrix from M)_{0..j-1};
if precision M < infinity then M = clean(tol,M);
);
if o.Reverse then M = matrix reverse(entries M);
M
)
TEST ///
N = matrix {{0.001, 0, 0}, {1, 1, 3}, {2, 2, 5.999}}
N = colReduce(N, Tolerance=>0.01)
assert(numcols N == 1)
///
--a list of column indices for a basis of the column space of M
basisIndices = (M, tol) -> (
M = new MutableMatrix from sub(M, coefficientRing ring M);--sub(M, ultimate(coefficientRing, ring M));
(m,n) := (numrows M, numcols M);
i := 0; --row of pivot
I := new MutableList;
for j from 0 to n-1 do (
if i == m then break;
a := if tol > 0 then i + maxPosition apply(i..m-1, l->(abs M_(l,j)))
else i + position(i..m-1, l -> M_(l,j) != 0);
c := M_(a,j);
if tol > 0 and abs c <= tol then continue;
I#(#I) = j;
rowSwap(M,a,i);
for l from 0 to n-1 do M_(i,l) = M_(i,l)/c; --rowMult(M,i,1/c); is bugged
for k from 0 to m-1 do rowAdd(M,k,-M_(k,j),i);
i = i+1;
);
new List from I
)
numericalImage = method(Options => {Tolerance => null})
numericalImage Matrix := o -> M -> (
R := ultimate(coefficientRing, ring M);
tol := getTolerance(R,o);
numericalImage(M,tol)
)
numericalImage (Matrix, Number) := o -> (M, tol) -> (
R := ultimate(coefficientRing, ring M);
M = sub(M, R);
if numcols M == 0 then return M;
if numrows M == 0 then return map(R^0,R^0,0);
if precision 1_(ring M) < infinity then (
(svs, U, Vt) := SVD M;
cols := positions(svs, sv->(sv > tol));
submatrix(U,,cols)
) else (
gens image M
)
)
TEST ///
M = matrix {{0.999, 2}, {1, 2}}
Mimage = numericalImage(M, 0.01)
assert(numcols Mimage == 1)
///
beginDocumentation()
doc ///
Key
NumericalLinearAlgebra
Headline
numerical linear algebra
Description
Text
This package collects implementations of numerical linear algebra algorithms.
@UL {
{TO numericalRank},
{TO numericalKernel},
{TO numericalImage},
{TO colReduce}
}@
///
doc ///
Key
Tolerance
Headline
the tolerance of a numerical computation
///
doc ///
Key
"Tolerance(NumericalLinearAlgebra)"
[numericalKernel,Tolerance]
[colReduce, Tolerance]
[numericalImage,Tolerance]
Headline
the tolerance of a numerical computation
Description
Text
The default value {\tt null} sets tolerance to 1e-6.
///
doc ///
Key
numericalKernel
(numericalKernel,Matrix)
Headline
approximate kernel of a matrix
Usage
V = numericalKernel(M)
Inputs
M:Matrix
Outputs
V:Matrix
Description
Text
Computes the kernel of a matrix M numerically using singular value decomposition.
Example
M = matrix {{1., 1, 1}}
numericalKernel(M, Tolerance=>0.01)
Text
Singular values less than the tolerance are treated as zero.
Example
M = matrix {{1., 1}, {1.001, 1}}
numericalKernel(M, Tolerance=>0.01)
///
document {
Key => {numericalRank, (numericalRank, Matrix), [numericalRank, Threshold],
isFullNumericalRank, (isFullNumericalRank,Matrix)},
Headline => "numerical rank of a matrix",
Usage => "r = numericalRank M\nB = isFullNumericalRank M",
Inputs => {
"M"=>Matrix=>"a matrix with real or complex entries"
},
Outputs => {
"r"=>ZZ,
"B"=>Boolean
},
PARA {
TO numericalRank, " finds an approximate rank of the matrix ", TT "M", "."
},
PARA {
TO isFullNumericalRank, " = ", TT "M", " is _not_ rank-deficient."
},
PARA {
"Let ", TEX "\\sigma_1,...,\\sigma_n", " be the singular values of ", TT "M", ". "
},
PARA {
"If ", TO "Threshold", " is >1, then to establish numerical rank we look
for the first large gap between two consecutive singular values. ",
"The gap between ", TEX "\\sigma_i", " and ", TEX "\\sigma_{i+1}",
" is large if ", TEX "\\sigma_i/\\sigma_{i+1} > ", TO "Threshold",
"."
},
PARA {
"If ", TO "Threshold", " is <=1, then the rank equals
the number of singular values larger then ", TO "Threshold", "."
},
Caveat => {"We assume ", TEX "\\sigma_0=1", " above."},
EXAMPLE lines ///
options numericalRank
numericalRank matrix {{2,1},{0,0.001}}
numericalRank matrix {{2,1},{0,0.0001}}
///,
SeeAlso => {SVD}
}
doc ///
Key
colReduce
(colReduce,Matrix)
[colReduce,Reverse]
[colReduce,Normalize]
Normalize
Headline
column reduce a matrix
Usage
N = colReduce M
Inputs
M:Matrix
Outputs
N:Matrix
in reduced column echelon form
Description
Text
Performs Gaussian column reduction on a matrix M, retaining only the linearly independent columns.
Example
M = matrix {{1., 2, 3}, {2, 4, 0}, {-1, -2, 3}}
colReduce(M, Tolerance=>0.01)
Text
Entries with absolute value below the tolerance are treated as zero and not used as pivots.
Example
N = matrix {{0.001, 0, 0}, {1, 1, 3}, {2, 2, 5.999}}
colReduce(N, Tolerance=>0.01)
Text
The lower rows are treated as the lead terms unless the optional argument {\tt Reverse} is set to true.
Example
colReduce(M, Reverse=>true)
Text
If the optional argument {\tt Normalize} is set to true (default) each vector is normalized so that the lead entry is 1. Otherwise this step is skipped.
Example
colReduce(M, Normalize=>false)
///
doc ///
Key
numericalImage
(numericalImage,Matrix,Number)
(numericalImage,Matrix)
Headline
Image of a matrix
Usage
V = numericalImage(M, tol)
Inputs
M:Matrix
tol:Number
a positive number, the numerical tolerance
Outputs
V:Matrix
Description
Text
Computes the image of a matrix M numerically using singular value decomposition.
Example
M = matrix {{1., 0, 1}, {0, 1, 1}, {1, 0, 1}}
numericalImage(M, 0.01)
Text
Singular values less than the tolerance are treated as zero.
Example
M = matrix {{0.999, 2}, {1, 2}}
numericalImage(M, 0.01)
///
|