Cholesky decomposition: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 82:
{{trans|Python}}
<
V l = [[0.0] * A.len] * A.len
L(i) 0 .< A.len
Line 106:
[54, 86, 174, 134],
[42, 62, 134, 106]]
pprint(cholesky(m2))</
{{out}}
Line 123:
{{works with|Ada 2005}}
decomposition.ads:
<
generic
with package Matrix is new Ada.Numerics.Generic_Real_Arrays (<>);
Line 131:
procedure Decompose (A : Matrix.Real_Matrix; L : out Matrix.Real_Matrix);
end Decomposition;</
decomposition.adb:
<
package body Decomposition is
Line 166:
end loop;
end Decompose;
end Decomposition;</
Example usage:
<
with Ada.Text_IO;
with Decomposition;
Line 213:
Ada.Text_IO.Put_Line ("A:"); Print (Example_2);
Ada.Text_IO.Put_Line ("L:"); Print (L_2);
end Decompose_Example;</
{{out}}
<pre>Example 1:
Line 242:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<
MODE FIELD=LONG REAL;
Line 300:
MAT c2 = cholesky(m2);
print matrix(c2)
)</
{{out}}
<pre>
Line 313:
=={{header|AutoHotkey}}==
<
L := [], n := A.Count()
L[1,1] := Sqrt(A[1,1])
Line 347:
}
return "[" Trim(output, "`n,") "]"
}</
Examples:<
, [15, 18, 0]
, [-5, 0 , 11]]
Line 360:
MsgBox % Result := ShowMatrix(L1) "`n----`n" ShowMatrix(L2) "`n----"
return</
{{out}}
<pre>[[5.000, 0.000, 0.000]
Line 374:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
m1() = 25, 15, -5, \
\ 15, 18, 0, \
Line 419:
PRINT
NEXT row%
ENDPROC</
'''Output:'''
<pre>
Line 433:
=={{header|C}}==
<
#include <stdlib.h>
#include <math.h>
Line 483:
return 0;
}</
{{out}}
<pre>5.00000 0.00000 0.00000
Line 495:
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
using System.Linq;
Line 597:
}
}
}</
{{out}}
Line 626:
=={{header|C++}}==
<
#include <cmath>
#include <iomanip>
Line 727:
return 0;
}</
{{out}}
Line 753:
=={{header|Clojure}}==
{{trans|Python}}
<
[matrix]
(let [n (count matrix)
Line 763:
(Math/sqrt (- (aget A i i) s))
(* (/ 1.0 (aget L j j)) (- (aget A i j) s))))))
(vec (map vec L))))</
Example:
<
;=> [[ 5.0 0.0 0.0]
; [ 3.0 3.0 0.0]
Line 774:
; [ 5.185449728701349 6.565905201197403 0.0 0.0 ]
; [12.727922061357857 3.0460384954008553 1.6497422479090704 0.0 ]
; [ 9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026]]</
=={{header|Common Lisp}}==
<
;; for a positive-definite, symmetric nxn matrix A.
(defun chol (A)
Line 805:
;; Return the calculated matrix L.
L))</
<
(setf A (make-array '(3 3) :initial-contents '((25 15 -5) (15 18 0) (-5 0 11))))
(chol A)
#2A((5.0 0 0)
(3.0 3.0 0)
(-1.0 1.0 3.0))</
<
(setf B (make-array '(4 4) :initial-contents '((18 22 54 42) (22 70 86 62) (54 86 174 134) (42 62 134 106))))
(chol B)
Line 820:
(5.18545 6.565905 0 0)
(12.727922 3.0460374 1.6497375 0)
(9.899495 1.6245536 1.849715 1.3926151))</
<
;; as above, returns the Cholesky decomposition matrix of a square positive-definite, symmetric matrix
(defun cholesky (m)
Line 832:
(setf (cdr (last l)) (list (nconc x (list (sqrt (- (elt cm (incf j)) (*v x x))))))))))
;; where *v is the scalar product defined as
(defun *v (v1 v2) (reduce #'+ (mapcar #'* v1 v2)))</
<
CL-USER> (setf a '((25 15 -5) (15 18 0) (-5 0 11)))
((25 15 -5) (15 18 0) (-5 0 11))
Line 843:
3 3 0
-1 1 3
NIL</
<
CL-USER> (setf a '((18 22 54 42) (22 70 86 62) (54 86 174 134) (42 62 134 106)))
((18 22 54 42) (22 70 86 62) (54 86 174 134) (42 62 134 106))
Line 855:
12.72792 3.04604 1.64974 0.00000
9.89950 1.62455 1.84971 1.39262
NIL</
=={{header|D}}==
<
T[][] cholesky(T)(in T[][] A) pure nothrow /*@safe*/ {
Line 884:
[42, 62, 134, 106]];
writefln("%(%(%2.3f %)\n%)", m2.cholesky);
}</
{{out}}
<pre> 5 0 0
Line 898:
=={{header|DWScript}}==
{{Trans|C}}
<
var
i, j, k, n : Integer;
Line 943:
42.0, 62.0, 134.0, 106.0 ];
var c2 := Cholesky(m2);
ShowMatrix(c2);</
=={{header|F_Sharp|F#}}==
<
let cholesky a =
Line 992:
printfn "ex2:"
printMat ex2
</syntaxhighlight>
{{out}}
<pre>ex1:
Line 1,006:
=={{header|Fantom}}==
<
** Cholesky decomposition
**
Line 1,060:
runTest ([[18f,22f,54f,42f],[22f,70f,86f,62f],[54f,86f,174f,134f],[42f,62f,134f,106f]])
}
}</
{{out}}
<pre>
Line 1,070:
=={{header|Fortran}}==
<
! *************************************************!
! LBH @ ULPGC 06/03/2014
Line 1,139:
end do
End program Cholesky_decomp</
{{out}}
<pre>
Line 1,149:
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<
' compile with: fbc -s console
Line 1,214:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> 5.00000 0.00000 0.00000
Line 1,228:
===Real===
This version works with real matrices, like most other solutions on the page. The representation is packed, however, storing only the lower triange of the input symetric matrix and the output lower matrix. The decomposition algorithm computes rows in order from top to bottom but is a little different thatn Cholesky–Banachiewicz.
<
import (
Line 1,332:
fmt.Println("L:")
a.choleskyLower().print()
}</
{{out}}
<pre>
Line 1,356:
===Hermitian===
This version handles complex Hermitian matricies as described on the WP page. The matrix representation is flat, and storage is allocated for all elements, not just the lower triangles. The decomposition algorithm is Cholesky–Banachiewicz.
<
import (
Line 1,430:
a.print(heading)
a.choleskyDecomp().print("Cholesky factor L:")
}</
{{out}}
<pre>
Line 1,465:
===Library gonum/mat===
<
import (
Line 1,491:
42, 62, 134, 106,
}))
}</
{{out}}
<pre>
Line 1,505:
===Library go.matrix===
<
import (
Line 1,537:
fmt.Println("L:")
fmt.Println(l)
}</
Output:
<pre>
Line 1,562:
=={{header|Groovy}}==
{{Trans|Java}}
<
assert a.size > 0 && a[0].size == a.size
def m = a.size
Line 1,575:
}
l
}</
Test:
<
[15, 18, 0],
[-5, 0, 11]]
Line 1,589:
println()
decompose(test).each { println it[0..<(test.size)] }
}</
{{out}}
<pre>[5.0, 0, 0]
Line 1,606:
The Cholesky module:
<
import Data.Array.IArray
Line 1,634:
return l
where maxBnd = fst . snd . bounds
update a l i = unsafeFreeze l >>= \l' -> writeArray l i (get a l' i)</
The main module:
<
import Data.List
import Cholesky
Line 1,665:
main = do
putStrLn $ showMatrice 3 $ elems $ cholesky ex1
putStrLn $ showMatrice 4 $ elems $ cholesky ex2</
<b>output:</b>
<pre>
Line 1,678:
===With Numeric.LinearAlgebra===
<
a,b :: Matrix R
Line 1,701:
print $ tr $ chol sb
</syntaxhighlight>
{{out}}
<pre>Herm (3><3)
Line 1,728:
=={{header|Icon}} and {{header|Unicon}}==
<
result := make_square_array (*array)
every (i := 1 to *array) do {
Line 1,768:
do_cholesky ([[25,15,-5],[15,18,0],[-5,0,11]])
do_cholesky ([[18,22,54,42],[22,70,86,62],[54,86,174,134],[42,62,134,106]])
end</
{{out}}
<pre>
Line 1,795:
'''Solution:'''
<
import Data.Vect
Line 1,862:
print ex2
putStrLn "\n"
</syntaxhighlight>
{{out}}
Line 1,873:
=={{header|J}}==
'''Solution:'''
<
h =: +@|: NB. conjugate transpose
Line 1,887:
L0,(T mp L0),.L1
end.
)</
See [[j:Essays/Cholesky Decomposition|Cholesky Decomposition essay]] on the J Wiki.
{{out|Examples}}
<
eg2=: 18 22 54 42 , 22 70 86 62 , 54 86 174 134 ,: 42 62 134 106
cholesky eg1
Line 1,900:
5.18545 6.56591 0 0
12.7279 3.04604 1.64974 0
9.89949 1.62455 1.84971 1.39262</
'''Using `math/lapack` addon'''
<
load 'math/lapack/potrf'
potrf_jlapack_ eg1
Line 1,912:
5.18545 6.56591 0 0
12.7279 3.04604 1.64974 0
9.89949 1.62455 1.84971 1.39262</
=={{header|Java}}==
{{works with|Java|1.5+}}
<
public class Cholesky {
Line 1,946:
System.out.println(Arrays.deepToString(chol(test2)));
}
}</
{{out}}
<pre>[[5.0, 0.0, 0.0], [3.0, 3.0, 0.0], [-1.0, 1.0, 3.0]]
Line 1,952:
=={{header|JavaScript}}==
<
const cholesky = function (array) {
const zeros = [...Array(array.length)].map( _ => Array(array.length).fill(0));
Line 1,966:
let arr4 = [[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]];
console.log(cholesky(arr4));
</syntaxhighlight>
{{output}}
Line 1,983:
{{Works with|jq|1.4}}
'''Infrastructure''':
<
def matrix(m; n; init):
if m == 0 then []
Line 2,021:
else [ ., 0, 0, length ] | test
end ;
</
if is_symmetric then
length as $length
Line 2,042:
end ))
else error( "cholesky_factor: matrix is not symmetric" )
end ;</
'''Task 1''':
[[25,15,-5],[15,18,0],[-5,0,11]] | cholesky_factor
Line 2,053:
[42, 62, 134, 106]] | cholesky_factor | neatly(20)
{{Out}}
<
5.185449728701349 6.565905201197403 0 0
12.727922061357857 3.0460384954008553 1.6497422479090704 0
9.899494936611665 1.6245538642137891 1.849711005231382 1.3926212476455924</
=={{header|Julia}}==
Julia's strong linear algebra support includes Cholesky decomposition.
<
a = [25 15 5; 15 18 0; -5 0 11]
b = [18 22 54 22; 22 70 86 62; 54 86 174 134; 42 62 134 106]
Line 2,066:
println(a, "\n => \n", chol(a, :L))
println(b, "\n => \n", chol(b, :L))
</syntaxhighlight>
{{out}}
Line 2,090:
=={{header|Kotlin}}==
{{trans|C}}
<
fun cholesky(a: DoubleArray): DoubleArray {
Line 2,129:
val c2 = cholesky(m2)
showMatrix(c2)
}</
{{out}}
Line 2,146:
{{trans|Go}}
Translated from the Go Real version: This version works with real matrices, like most other solutions on the page. The representation is packed, however, storing only the lower triange of the input symetric matrix and the output lower matrix. The decomposition algorithm computes rows in order from top to bottom but is a little different than Cholesky–Banachiewicz.
<
// choleskyLower returns the cholesky decomposition of a symmetric real
Line 2,225:
54.0, 86.0, 174.0,
42.0, 62.0, 134.0, 106.0])
</syntaxhighlight>
{{out}}
<pre>
Line 2,250:
=={{header|Maple}}==
The Cholesky decomposition is obtained by passing the `method = Cholesky' option to the LUDecomposition procedure in the LinearAlgebra pacakge. This is illustrated below for the two requested examples. The first is computed exactly; the second is also, but the subsequent application of `evalf' to the result produces a matrix with floating point entries which can be compared with the expected output in the problem statement.
<
[25 15 -5]
[ ]
Line 2,301:
[12.72792206 3.046038495 1.649742248 0. ]
[ ]
[9.899494934 1.624553864 1.849711006 1.392621248]</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Without the use of built-in functions, making use of memoization:
<
Module[{L},
L[k_, k_] := L[k, k] = Sqrt[A[[k, k]] - Sum[L[k, j]^2, {j, 1, k-1}]];
L[i_, k_] := L[i, k] = L[k, k]^-1 (A[[i, k]] - Sum[L[i, j] L[k, j], {j, 1, k-1}]);
PadRight[Table[L[i, j], {i, Length[A]}, {j, i}]]
]</
=={{header|MATLAB}} / {{header|Octave}}==
The cholesky decomposition chol() is an internal function
<
25 15 -5
15 18 0
Line 2,328:
[L] = chol(A,'lower')
[L] = chol(B,'lower')
</syntaxhighlight>
{{out}}
<pre> > [L] = chol(A,'lower')
Line 2,346:
=={{header|Maxima}}==
<
a: hilbert_matrix(4)$
Line 2,357:
b . transpose(b) - a;
matrix([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0])</
=={{header|Nim}}==
{{trans|C}}
<
type Matrix[N: static int, T: SomeFloat] = array[N, array[N, T]]
Line 2,392:
[54.0, 86.0, 174.0, 134.0],
[42.0, 62.0, 134.0, 106.0]]
echo cholesky(m2)</
{{out}}
Line 2,407:
{{trans|C}}
<
class Cholesky {
function : Main(args : String[]) ~ Nil {
Line 2,451:
}
}
</syntaxhighlight>
<pre>
Line 2,465:
=={{header|OCaml}}==
<
let n = Array.length inp in
let res = Array.make_matrix n n 0.0 in
Line 2,494:
[|22.0; 70.0; 86.0; 62.0|];
[|54.0; 86.0; 174.0; 134.0|];
[|42.0; 62.0; 134.0; 106.0|] |];</
{{out}}
<pre>in:
Line 2,518:
=={{header|ooRexx}}==
{{trans|REXX}}
<
niner = '25 15 -5' , /*define a 3x3 matrix. */
'15 18 0' ,
Line 2,568:
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return (g/1)i /*make complex if X < 0.*/</
=={{header|PARI/GP}}==
<
{
my (L = matrix(#M,#M));
Line 2,583:
);
L
}</
Output: (set displayed digits with: \p 5)
Line 2,607:
=={{header|Pascal}}==
<
type
Line 2,674:
cOut := cholesky(cIn);
printM(cOut);
end.</
{{out}}
<pre>
Line 2,688:
=={{header|Perl}}==
<
my $matrix = shift;
my $chol = [ map { [(0) x @$matrix ] } @$matrix ];
Line 2,713:
print "\nExample 2:\n";
print +(map { sprintf "%7.4f\t", $_ } @$_), "\n" for @{ cholesky $example2 };
</syntaxhighlight>
{{out}}
<pre>
Line 2,730:
=={{header|Phix}}==
{{trans|Sidef}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">cholesky</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">matrix</span><span style="color: #0000FF;">)</span>
Line 2,755:
<span style="color: #0000FF;">{</span> <span style="color: #000000;">54</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">86</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">174</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">134</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">42</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">62</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">134</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">106</span><span style="color: #0000FF;">}}))</span>
<!--</
{{out}}
<pre>
Line 2,768:
=={{header|PicoLisp}}==
<
(load "@lib/math.l")
Line 2,784:
(for R L
(for N R (prin (align 9 (round N 5))))
(prinl) ) ) )</
Test:
<
'((25.0 15.0 -5.0) (15.0 18.0 0) (-5.0 0 11.0)) )
Line 2,796:
(22.0 70.0 86.0 62.0)
(54.0 86.0 174.0 134.0)
(42.0 62.0 134.0 106.0) ) )</
{{out}}
<pre> 5.00000 0.00000 0.00000
Line 2,808:
=={{header|PL/I}}==
<
decompose: procedure options (main); /* 31 October 2013 */
declare a(*,*) float controlled;
Line 2,854:
end cholesky;
end decompose;</
ACTUAL RESULTS:-
<pre>Original matrix:
Line 2,877:
=={{header|PowerShell}}==
<
function cholesky ($a) {
$l = @()
Line 2,929:
"l2 ="
show (cholesky $a2)
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,957:
=={{header|Python}}==
===Python2.X version===
<
from pprint import pprint
Line 2,983:
[54, 86, 174, 134],
[42, 62, 134, 106]]
pprint(cholesky(m2), width=120)</
{{out}}
Line 2,997:
Factors out accesses to <code>A[i], L[i], and L[j]</code> by creating <code>Ai, Li and Lj</code> respectively as well as using <code>enumerate</code> instead of <code>range(len(some_array))</code>.
<
L = [[0.0] * len(A) for _ in range(len(A))]
for i, (Ai, Li) in enumerate(zip(A, L)):
Line 3,004:
Li[j] = sqrt(Ai[i] - s) if (i == j) else \
(1.0 / Lj[j] * (Ai[j] - s))
return L</
{{out}}
Line 3,011:
=={{header|q}}==
<
ak:{[m;k] (),/:m[;k]til k:k-1}
akk:{[m;k] m[k;k:k-1]}
Line 3,026:
show cholesky (25 15 -5f;15 18 0f;-5 0 11f)
-1"";
show cholesky (18 22 54 42f;22 70 86 62f;54 86 174 134f;42 62 134 106f)</
{{out}}
Line 3,040:
=={{header|R}}==
<
# [,1] [,2] [,3]
# [1,] 5 0 0
Line 3,051:
# [2,] 5.185450 6.565905 0.000000 0.000000
# [3,] 12.727922 3.046038 1.649742 0.000000
# [4,] 9.899495 1.624554 1.849711 1.392621</
=={{header|Racket}}==
<
#lang racket
(require math)
Line 3,083:
[54 86 174 134]
[42 62 134 106]]))
</syntaxhighlight>
Output:
<
'#(#(5 0 0)
#(3 3 0)
Line 3,094:
#( 9.899494936611665 1.6245538642137891 1.849711005231382 1.3926212476455924))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang=raku
my @L = @A »*» 0;
for ^@A -> $i {
Line 3,121:
[54, 86, 174, 134],
[42, 62, 134, 106],
];</
=={{header|REXX}}==
Line 3,127:
<br>REXX number normalization) can be removed from the line (number 40) which contains the source statement:
::::: <b> z=z right( format(@.row.col, , dPlaces) / 1, width) </b>
<
niner = '25 15 -5' , /*define a 3x3 matrix with elements. */
'15 18 0' ,
Line 3,175:
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_ %2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g/1</
{{out|output}}
<pre>
Line 3,208:
=={{header|Ring}}==
<
# Project : Cholesky decomposition
Line 3,251:
see nl
next
</syntaxhighlight>
Output:
<pre>
Line 3,265:
=={{header|Ruby}}==
<
class Matrix
Line 3,302:
[22, 70, 86, 62],
[54, 86, 174, 134],
[42, 62, 134, 106]].cholesky_factor</
{{out}}
<pre>
Line 3,315:
{{trans|C}}
<
let mut res = vec![0.0; mat.len()];
for i in 0..n {
Line 3,355:
show_matrix(res2, dimension);
}
</syntaxhighlight>
{{out}}
Line 3,370:
=={{header|Scala}}==
<
// Assuming matrix is positive-definite, symmetric and not empty...
Line 3,430:
(l2.matrix.flatten.zip(r2)).foreach{ case (result,test) =>
assert(math.round( result * 100000 ) * 0.00001 == math.round( test * 100000 ) * 0.00001)
}</
=={{header|Scilab}}==
Line 3,436:
The Cholesky decomposition is builtin, and an upper triangular matrix is returned, such that $A=L^TL$.
<
chol(a)
ans =
Line 3,454:
0. 6.5659052 3.0460385 1.6245539
0. 0. 1.6497422 1.849711
0. 0. 0. 1.3926212</
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 3,516:
writeln;
writeMat(cholesky(m2));
end func;</
Output:
Line 3,532:
=={{header|Sidef}}==
{{trans|Perl}}
<
var chol = matrix.len.of { matrix.len.of(0) }
for row in ^matrix {
Line 3,544:
}
return chol
}</
Examples:
<
[ 15, 18, 0 ],
[ -5, 0, 11 ] ];
Line 3,564:
cholesky(example2).each { |row|
say row.map {'%7.4f' % _}.join(' ');
}</
{{out}}
<pre>
Line 3,580:
=={{header|Smalltalk}}==
<
FloatMatrix>>#cholesky
| l |
Line 3,603:
l at: k @ i put: aki - partialSum * factor reciprocal]]].
^l
</syntaxhighlight>
=={{header|Stata}}==
See [http://www.stata.com/help.cgi?mf_cholesky Cholesky square-root decomposition] in Stata help.
<
: a=25,15,-5\15,18,0\-5,0,11
Line 3,646:
3 | 12.72792206 3.046038495 1.649742248 0 |
4 | 9.899494937 1.624553864 1.849711005 1.392621248 |
+---------------------------------------------------------+</
=={{header|Swift}}==
{{trans|Rust}}
<
var res = [Double](repeating: 0, count: matrix.count)
Line 3,700:
printMatrix(res1, n: 3)
print()
printMatrix(res2, n: 4)</
{{out}}
Line 3,714:
=={{header|Tcl}}==
{{trans|Java}}
<
set m [llength $a]
set n [llength [lindex $a 0]]
Line 3,732:
}
return $l
}</
Demonstration code:
<
{25 15 -5}
{15 18 0}
Line 3,746:
{42 62 134 106}
}
puts [cholesky $test2]</
{{out}}
<pre>
Line 3,756:
This function returns the lower Cholesky decomposition of a square matrix fed to it. It does not check for positive semi-definiteness, although it does check for squareness. It assumes that <code>Option Base 0</code> is set, and thus the matrix entry indices need to be adjusted if Base is set to 1. It also assumes a matrix of size less than 256x256. To handle larger matrices, change all <code>Byte</code>-type variables to <code>Long</code>. It takes the square matrix range as an input, and can be implemented as an array function on the same sized square range of cells as output. For example, if the matrix is in cells A1:E5, highlighting cells A10:E14, typing "<code>=Cholesky(A1:E5)</code>" and htting <code>Ctrl-Shift-Enter</code> will populate the target cells with the lower Cholesky decomposition.
<
Dim A() As Double, L() As Double, sum As Double, sum2 As Double
Line 3,808:
Cholesky = L
End Function
</syntaxhighlight>
=={{header|Vlang}}==
{{trans|go}}
<
// Symmetric and Lower use a packed representation that stores only
Line 3,910:
println("L:")
a.cholesky_lower().print()
}</
{{out}}
Line 3,937:
{{libheader|Wren-matrix}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 3,957:
Fmt.mprint(Matrix.new(array).cholesky(), 8, 5)
System.print()
}</
{{out}}
Line 3,986:
=={{header|zkl}}==
Using the GNU Scientific Library:
<
fcn lowerCholesky(m){ // trans: C
rows:=m.rows;
Line 3,996:
}
lcm
}</
{{out}}
<pre>
Line 4,022:
Or, using lists:
{{trans|C}}
<
rows:=mat.len();
r:=(0).pump(rows,List().write, (0).pump(rows,List,0.0).copy); // matrix of zeros
Line 4,031:
}
r
}</
<
printM(cholesky(ex1));
println("-----------------");
Line 4,039:
L(54.0, 86.0, 174.0, 134.0,),
L(42.0, 62.0, 134.0, 106.0,) );
printM(cholesky(ex2));</
<
fcn rowFmt(row){ ("%9.5f "*row.len()).fmt(row.xplode()) }</
{{out}}
<pre>
Line 4,056:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
<
20 LET d=3000: GO SUB 1000: GO SUB 4000: GO SUB 5000
30 STOP
Line 4,090:
5050 PRINT
5060 NEXT r
5070 RETURN</
|