Cholesky decomposition: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: oops, add missing '%') |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 78: | Line 78: | ||
# The Cholesky decomposition of a [[Pascal matrix generation|Pascal]] upper-triangle matrix is the [[wp:Identity matrix|Identity matrix]] of the same size. |
# The Cholesky decomposition of a [[Pascal matrix generation|Pascal]] upper-triangle matrix is the [[wp:Identity matrix|Identity matrix]] of the same size. |
||
# The Cholesky decomposition of a Pascal symmetric matrix is the Pascal lower-triangle matrix of the same size. |
# The Cholesky decomposition of a Pascal symmetric matrix is the Pascal lower-triangle matrix of the same size. |
||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=11l>F cholesky(A) |
<syntaxhighlight lang="11l">F cholesky(A) |
||
V l = [[0.0] * A.len] * A.len |
V l = [[0.0] * A.len] * A.len |
||
L(i) 0 .< A.len |
L(i) 0 .< A.len |
||
Line 119: | Line 118: | ||
] |
] |
||
</pre> |
</pre> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|Ada 2005}} |
{{works with|Ada 2005}} |
||
decomposition.ads: |
decomposition.ads: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Numerics.Generic_Real_Arrays; |
||
generic |
generic |
||
with package Matrix is new Ada.Numerics.Generic_Real_Arrays (<>); |
with package Matrix is new Ada.Numerics.Generic_Real_Arrays (<>); |
||
Line 134: | Line 132: | ||
decomposition.adb: |
decomposition.adb: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Numerics.Generic_Elementary_Functions; |
||
package body Decomposition is |
package body Decomposition is |
||
Line 169: | Line 167: | ||
Example usage: |
Example usage: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Numerics.Real_Arrays; |
||
with Ada.Text_IO; |
with Ada.Text_IO; |
||
with Decomposition; |
with Decomposition; |
||
Line 236: | Line 234: | ||
12.728 3.046 1.650 0.000 |
12.728 3.046 1.650 0.000 |
||
9.899 1.625 1.850 1.393</pre> |
9.899 1.625 1.850 1.393</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{trans|C}} Note: This specimen retains the original [[#C|C]] coding style. [http://rosettacode.org/mw/index.php?title=Cholesky_decomposition&action=historysubmit&diff=107753&oldid=107752 diff] |
{{trans|C}} Note: This specimen retains the original [[#C|C]] coding style. [http://rosettacode.org/mw/index.php?title=Cholesky_decomposition&action=historysubmit&diff=107753&oldid=107752 diff] |
||
Line 242: | Line 239: | ||
{{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].}} |
{{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''.}} |
{{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''.}} |
||
<syntaxhighlight lang=algol68>#!/usr/local/bin/a68g --script # |
<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script # |
||
MODE FIELD=LONG REAL; |
MODE FIELD=LONG REAL; |
||
Line 311: | Line 308: | ||
( 9.89949, 1.62455, 1.84971, 1.39262)) |
( 9.89949, 1.62455, 1.84971, 1.39262)) |
||
</pre> |
</pre> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="autohotkey">Cholesky_Decomposition(A){ |
||
L := [], n := A.Count() |
L := [], n := A.Count() |
||
L[1,1] := Sqrt(A[1,1]) |
L[1,1] := Sqrt(A[1,1]) |
||
Line 348: | Line 344: | ||
return "[" Trim(output, "`n,") "]" |
return "[" Trim(output, "`n,") "]" |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Examples:<syntaxhighlight lang= |
Examples:<syntaxhighlight lang="autohotkey">A := [[25, 15, -5] |
||
, [15, 18, 0] |
, [15, 18, 0] |
||
, [-5, 0 , 11]] |
, [-5, 0 , 11]] |
||
Line 371: | Line 367: | ||
,[9.899, 1.625, 1.850, 1.393]] |
,[9.899, 1.625, 1.850, 1.393]] |
||
----</pre> |
----</pre> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
<syntaxhighlight lang=bbcbasic> DIM m1(2,2) |
<syntaxhighlight lang="bbcbasic"> DIM m1(2,2) |
||
m1() = 25, 15, -5, \ |
m1() = 25, 15, -5, \ |
||
\ 15, 18, 0, \ |
\ 15, 18, 0, \ |
||
Line 431: | Line 426: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang=c>#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 493: | Line 487: | ||
12.72792 3.04604 1.64974 0.00000 |
12.72792 3.04604 1.64974 0.00000 |
||
9.89949 1.62455 1.84971 1.39262</pre> |
9.89949 1.62455 1.84971 1.39262</pre> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 624: | Line 617: | ||
12.72792, 3.04604, 1.64974, 0.00000, |
12.72792, 3.04604, 1.64974, 0.00000, |
||
9.89949, 1.62455, 1.84971, 1.39262, |
9.89949, 1.62455, 1.84971, 1.39262, |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang=cpp>#include <cassert> |
<syntaxhighlight lang="cpp">#include <cassert> |
||
#include <cmath> |
#include <cmath> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 750: | Line 742: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=clojure>(defn cholesky |
<syntaxhighlight lang="clojure">(defn cholesky |
||
[matrix] |
[matrix] |
||
(let [n (count matrix) |
(let [n (count matrix) |
||
Line 765: | Line 756: | ||
(vec (map vec L))))</syntaxhighlight> |
(vec (map vec L))))</syntaxhighlight> |
||
Example: |
Example: |
||
<syntaxhighlight lang=clojure>(cholesky [[25 15 -5] [15 18 0] [-5 0 11]]) |
<syntaxhighlight lang="clojure">(cholesky [[25 15 -5] [15 18 0] [-5 0 11]]) |
||
;=> [[ 5.0 0.0 0.0] |
;=> [[ 5.0 0.0 0.0] |
||
; [ 3.0 3.0 0.0] |
; [ 3.0 3.0 0.0] |
||
Line 775: | Line 766: | ||
; [12.727922061357857 3.0460384954008553 1.6497422479090704 0.0 ] |
; [12.727922061357857 3.0460384954008553 1.6497422479090704 0.0 ] |
||
; [ 9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026]]</syntaxhighlight> |
; [ 9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026]]</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang=lisp>;; Calculates the Cholesky decomposition matrix L |
<syntaxhighlight lang="lisp">;; Calculates the Cholesky decomposition matrix L |
||
;; for a positive-definite, symmetric nxn matrix A. |
;; for a positive-definite, symmetric nxn matrix A. |
||
(defun chol (A) |
(defun chol (A) |
||
Line 807: | Line 797: | ||
L))</syntaxhighlight> |
L))</syntaxhighlight> |
||
<syntaxhighlight lang=lisp>;; Example 1: |
<syntaxhighlight lang="lisp">;; Example 1: |
||
(setf A (make-array '(3 3) :initial-contents '((25 15 -5) (15 18 0) (-5 0 11)))) |
(setf A (make-array '(3 3) :initial-contents '((25 15 -5) (15 18 0) (-5 0 11)))) |
||
(chol A) |
(chol A) |
||
Line 814: | Line 804: | ||
(-1.0 1.0 3.0))</syntaxhighlight> |
(-1.0 1.0 3.0))</syntaxhighlight> |
||
<syntaxhighlight lang=lisp>;; Example 2: |
<syntaxhighlight lang="lisp">;; Example 2: |
||
(setf B (make-array '(4 4) :initial-contents '((18 22 54 42) (22 70 86 62) (54 86 174 134) (42 62 134 106)))) |
(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) |
(chol B) |
||
Line 822: | Line 812: | ||
(9.899495 1.6245536 1.849715 1.3926151))</syntaxhighlight> |
(9.899495 1.6245536 1.849715 1.3926151))</syntaxhighlight> |
||
<syntaxhighlight lang=lisp>;; case of matrix stored as a list of lists (inner lists are rows of matrix) |
<syntaxhighlight lang="lisp">;; case of matrix stored as a list of lists (inner lists are rows of matrix) |
||
;; as above, returns the Cholesky decomposition matrix of a square positive-definite, symmetric matrix |
;; as above, returns the Cholesky decomposition matrix of a square positive-definite, symmetric matrix |
||
(defun cholesky (m) |
(defun cholesky (m) |
||
Line 834: | Line 824: | ||
(defun *v (v1 v2) (reduce #'+ (mapcar #'* v1 v2)))</syntaxhighlight> |
(defun *v (v1 v2) (reduce #'+ (mapcar #'* v1 v2)))</syntaxhighlight> |
||
<syntaxhighlight lang=lisp>;; example 1 |
<syntaxhighlight lang="lisp">;; example 1 |
||
CL-USER> (setf a '((25 15 -5) (15 18 0) (-5 0 11))) |
CL-USER> (setf a '((25 15 -5) (15 18 0) (-5 0 11))) |
||
((25 15 -5) (15 18 0) (-5 0 11)) |
((25 15 -5) (15 18 0) (-5 0 11)) |
||
Line 845: | Line 835: | ||
NIL</syntaxhighlight> |
NIL</syntaxhighlight> |
||
<syntaxhighlight lang=lisp>;; example 2 |
<syntaxhighlight lang="lisp">;; example 2 |
||
CL-USER> (setf a '((18 22 54 42) (22 70 86 62) (54 86 174 134) (42 62 134 106))) |
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)) |
((18 22 54 42) (22 70 86 62) (54 86 174 134) (42 62 134 106)) |
||
Line 856: | Line 846: | ||
9.89950 1.62455 1.84971 1.39262 |
9.89950 1.62455 1.84971 1.39262 |
||
NIL</syntaxhighlight> |
NIL</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang=d>import std.stdio, std.math, std.numeric; |
<syntaxhighlight lang="d">import std.stdio, std.math, std.numeric; |
||
T[][] cholesky(T)(in T[][] A) pure nothrow /*@safe*/ { |
T[][] cholesky(T)(in T[][] A) pure nothrow /*@safe*/ { |
||
Line 898: | Line 887: | ||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
{{Trans|C}} |
{{Trans|C}} |
||
<syntaxhighlight lang=delphi>function Cholesky(a : array of Float) : array of Float; |
<syntaxhighlight lang="delphi">function Cholesky(a : array of Float) : array of Float; |
||
var |
var |
||
i, j, k, n : Integer; |
i, j, k, n : Integer; |
||
Line 944: | Line 933: | ||
var c2 := Cholesky(m2); |
var c2 := Cholesky(m2); |
||
ShowMatrix(c2);</syntaxhighlight> |
ShowMatrix(c2);</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
<syntaxhighlight lang=fsharp>open Microsoft.FSharp.Collections |
<syntaxhighlight lang="fsharp">open Microsoft.FSharp.Collections |
||
let cholesky a = |
let cholesky a = |
||
Line 1,004: | Line 992: | ||
42.00000, 62.00000, 134.00000, 106.00000, 9.89949, 1.62455, 1.84971, 1.39262, |
42.00000, 62.00000, 134.00000, 106.00000, 9.89949, 1.62455, 1.84971, 1.39262, |
||
</pre> |
</pre> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
<syntaxhighlight lang=fantom>** |
<syntaxhighlight lang="fantom">** |
||
** Cholesky decomposition |
** Cholesky decomposition |
||
** |
** |
||
Line 1,068: | Line 1,055: | ||
[[4.242640687119285, 0.0, 0.0, 0.0], [5.185449728701349, 6.565905201197403, 0.0, 0.0], [12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0.0], [9.899494936611667, 1.624553864213788, 1.8497110052313648, 1.3926212476456026]] |
[[4.242640687119285, 0.0, 0.0, 0.0], [5.185449728701349, 6.565905201197403, 0.0, 0.0], [12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0.0], [9.899494936611667, 1.624553864213788, 1.8497110052313648, 1.3926212476456026]] |
||
</pre> |
</pre> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="fortran">Program Cholesky_decomp |
||
! *************************************************! |
! *************************************************! |
||
! LBH @ ULPGC 06/03/2014 |
! LBH @ ULPGC 06/03/2014 |
||
Line 1,146: | Line 1,132: | ||
-1.0 1.0 3.0 |
-1.0 1.0 3.0 |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
<syntaxhighlight lang=freebasic>' version 18-01-2017 |
<syntaxhighlight lang="freebasic">' version 18-01-2017 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,224: | Line 1,209: | ||
12.72792 3.04604 1.64974 0.00000 |
12.72792 3.04604 1.64974 0.00000 |
||
9.89949 1.62455 1.84971 1.39262</pre> |
9.89949 1.62455 1.84971 1.39262</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Real=== |
===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. |
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. |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,356: | Line 1,340: | ||
===Hermitian=== |
===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. |
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. |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,465: | Line 1,449: | ||
===Library gonum/mat=== |
===Library gonum/mat=== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,505: | Line 1,489: | ||
===Library go.matrix=== |
===Library go.matrix=== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,559: | Line 1,543: | ||
9.899495, 1.624554, 1.849711, 1.392621} |
9.899495, 1.624554, 1.849711, 1.392621} |
||
</pre> |
</pre> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{Trans|Java}} |
{{Trans|Java}} |
||
<syntaxhighlight lang=groovy>def decompose = { a -> |
<syntaxhighlight lang="groovy">def decompose = { a -> |
||
assert a.size > 0 && a[0].size == a.size |
assert a.size > 0 && a[0].size == a.size |
||
def m = a.size |
def m = a.size |
||
Line 1,577: | Line 1,560: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Test: |
Test: |
||
<syntaxhighlight lang=groovy>def test1 = [[25, 15, -5], |
<syntaxhighlight lang="groovy">def test1 = [[25, 15, -5], |
||
[15, 18, 0], |
[15, 18, 0], |
||
[-5, 0, 11]] |
[-5, 0, 11]] |
||
Line 1,599: | Line 1,582: | ||
[12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0] |
[12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0] |
||
[9.899494936611667, 1.624553864213788, 1.8497110052313648, 1.3926212476456026]</pre> |
[9.899494936611667, 1.624553864213788, 1.8497110052313648, 1.3926212476456026]</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
We use the [http://en.wikipedia.org/wiki/Cholesky_decomposition#The_Cholesky.E2.80.93Banachiewicz_and_Cholesky.E2.80.93Crout_algorithms Cholesky–Banachiewicz algorithm] described in the Wikipedia article. |
We use the [http://en.wikipedia.org/wiki/Cholesky_decomposition#The_Cholesky.E2.80.93Banachiewicz_and_Cholesky.E2.80.93Crout_algorithms Cholesky–Banachiewicz algorithm] described in the Wikipedia article. |
||
Line 1,606: | Line 1,588: | ||
The Cholesky module: |
The Cholesky module: |
||
<syntaxhighlight lang=haskell>module Cholesky (Arr, cholesky) where |
<syntaxhighlight lang="haskell">module Cholesky (Arr, cholesky) where |
||
import Data.Array.IArray |
import Data.Array.IArray |
||
Line 1,636: | Line 1,618: | ||
update a l i = unsafeFreeze l >>= \l' -> writeArray l i (get a l' i)</syntaxhighlight> |
update a l i = unsafeFreeze l >>= \l' -> writeArray l i (get a l' i)</syntaxhighlight> |
||
The main module: |
The main module: |
||
<syntaxhighlight lang=haskell>import Data.Array.IArray |
<syntaxhighlight lang="haskell">import Data.Array.IArray |
||
import Data.List |
import Data.List |
||
import Cholesky |
import Cholesky |
||
Line 1,678: | Line 1,660: | ||
===With Numeric.LinearAlgebra=== |
===With Numeric.LinearAlgebra=== |
||
<syntaxhighlight lang=haskell>import Numeric.LinearAlgebra |
<syntaxhighlight lang="haskell">import Numeric.LinearAlgebra |
||
a,b :: Matrix R |
a,b :: Matrix R |
||
Line 1,726: | Line 1,708: | ||
, 12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0.0 |
, 12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0.0 |
||
, 9.899494936611665, 1.6245538642137891, 1.849711005231382, 1.3926212476455904 ]</pre> |
, 9.899494936611665, 1.6245538642137891, 1.849711005231382, 1.3926212476455904 ]</pre> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="icon">procedure cholesky (array) |
||
result := make_square_array (*array) |
result := make_square_array (*array) |
||
every (i := 1 to *array) do { |
every (i := 1 to *array) do { |
||
Line 1,790: | Line 1,771: | ||
9.899494937 1.624553864 1.849711005 1.392621248 |
9.899494937 1.624553864 1.849711005 1.392621248 |
||
</pre> |
</pre> |
||
=={{header|Idris}}== |
=={{header|Idris}}== |
||
'''works with Idris 0.10''' |
'''works with Idris 0.10''' |
||
'''Solution:''' |
'''Solution:''' |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="idris">module Main |
||
import Data.Vect |
import Data.Vect |
||
Line 1,870: | Line 1,850: | ||
[[4.242640687119285, 0, 0, 0], [5.185449728701349, 6.565905201197403, 0, 0], [12.72792206135786, 3.046038495400855, 1.64974224790907, 0], [9.899494936611665, 1.624553864213789, 1.849711005231382, 1.392621247645587]] |
[[4.242640687119285, 0, 0, 0], [5.185449728701349, 6.565905201197403, 0, 0], [12.72792206135786, 3.046038495400855, 1.64974224790907, 0], [9.899494936611665, 1.624553864213789, 1.849711005231382, 1.392621247645587]] |
||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
<syntaxhighlight lang=j>mp=: +/ . * NB. matrix product |
<syntaxhighlight lang="j">mp=: +/ . * NB. matrix product |
||
h =: +@|: NB. conjugate transpose |
h =: +@|: NB. conjugate transpose |
||
Line 1,890: | Line 1,869: | ||
See [[j:Essays/Cholesky Decomposition|Cholesky Decomposition essay]] on the J Wiki. |
See [[j:Essays/Cholesky Decomposition|Cholesky Decomposition essay]] on the J Wiki. |
||
{{out|Examples}} |
{{out|Examples}} |
||
<syntaxhighlight lang=j> eg1=: 25 15 _5 , 15 18 0 ,: _5 0 11 |
<syntaxhighlight lang="j"> eg1=: 25 15 _5 , 15 18 0 ,: _5 0 11 |
||
eg2=: 18 22 54 42 , 22 70 86 62 , 54 86 174 134 ,: 42 62 134 106 |
eg2=: 18 22 54 42 , 22 70 86 62 , 54 86 174 134 ,: 42 62 134 106 |
||
cholesky eg1 |
cholesky eg1 |
||
Line 1,902: | Line 1,881: | ||
9.89949 1.62455 1.84971 1.39262</syntaxhighlight> |
9.89949 1.62455 1.84971 1.39262</syntaxhighlight> |
||
'''Using `math/lapack` addon''' |
'''Using `math/lapack` addon''' |
||
<syntaxhighlight lang=j> load 'math/lapack' |
<syntaxhighlight lang="j"> load 'math/lapack' |
||
load 'math/lapack/potrf' |
load 'math/lapack/potrf' |
||
potrf_jlapack_ eg1 |
potrf_jlapack_ eg1 |
||
Line 1,913: | Line 1,892: | ||
12.7279 3.04604 1.64974 0 |
12.7279 3.04604 1.64974 0 |
||
9.89949 1.62455 1.84971 1.39262</syntaxhighlight> |
9.89949 1.62455 1.84971 1.39262</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
<syntaxhighlight lang=java5>import java.util.Arrays; |
<syntaxhighlight lang="java5">import java.util.Arrays; |
||
public class Cholesky { |
public class Cholesky { |
||
Line 1,950: | Line 1,928: | ||
<pre>[[5.0, 0.0, 0.0], [3.0, 3.0, 0.0], [-1.0, 1.0, 3.0]] |
<pre>[[5.0, 0.0, 0.0], [3.0, 3.0, 0.0], [-1.0, 1.0, 3.0]] |
||
[[4.242640687119285, 0.0, 0.0, 0.0], [5.185449728701349, 6.565905201197403, 0.0, 0.0], [12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0.0], [9.899494936611667, 1.624553864213788, 1.8497110052313648, 1.3926212476456026]]</pre> |
[[4.242640687119285, 0.0, 0.0, 0.0], [5.185449728701349, 6.565905201197403, 0.0, 0.0], [12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0.0], [9.899494936611667, 1.624553864213788, 1.8497110052313648, 1.3926212476456026]]</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<syntaxhighlight lang=javascript> |
<syntaxhighlight lang="javascript"> |
||
const cholesky = function (array) { |
const cholesky = function (array) { |
||
const zeros = [...Array(array.length)].map( _ => Array(array.length).fill(0)); |
const zeros = [...Array(array.length)].map( _ => Array(array.length).fill(0)); |
||
Line 1,979: | Line 1,956: | ||
3: (4) [9.899494936611665, 1.6245538642137891, 1.849711005231382, 1.3926212476455924] |
3: (4) [9.899494936611665, 1.6245538642137891, 1.849711005231382, 1.3926212476455924] |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{Works with|jq|1.4}} |
{{Works with|jq|1.4}} |
||
'''Infrastructure''': |
'''Infrastructure''': |
||
<syntaxhighlight lang=jq># Create an m x n matrix |
<syntaxhighlight lang="jq"># Create an m x n matrix |
||
def matrix(m; n; init): |
def matrix(m; n; init): |
||
if m == 0 then [] |
if m == 0 then [] |
||
Line 2,021: | Line 1,997: | ||
else [ ., 0, 0, length ] | test |
else [ ., 0, 0, length ] | test |
||
end ; |
end ; |
||
</syntaxhighlight>'''Cholesky Decomposition''':<syntaxhighlight lang=jq>def cholesky_factor: |
</syntaxhighlight>'''Cholesky Decomposition''':<syntaxhighlight lang="jq">def cholesky_factor: |
||
if is_symmetric then |
if is_symmetric then |
||
length as $length |
length as $length |
||
Line 2,053: | Line 2,029: | ||
[42, 62, 134, 106]] | cholesky_factor | neatly(20) |
[42, 62, 134, 106]] | cholesky_factor | neatly(20) |
||
{{Out}} |
{{Out}} |
||
<syntaxhighlight lang=jq> 4.242640687119285 0 0 0 |
<syntaxhighlight lang="jq"> 4.242640687119285 0 0 0 |
||
5.185449728701349 6.565905201197403 0 0 |
5.185449728701349 6.565905201197403 0 0 |
||
12.727922061357857 3.0460384954008553 1.6497422479090704 0 |
12.727922061357857 3.0460384954008553 1.6497422479090704 0 |
||
9.899494936611665 1.6245538642137891 1.849711005231382 1.3926212476455924</syntaxhighlight> |
9.899494936611665 1.6245538642137891 1.849711005231382 1.3926212476455924</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Julia's strong linear algebra support includes Cholesky decomposition. |
Julia's strong linear algebra support includes Cholesky decomposition. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="julia"> |
||
a = [25 15 5; 15 18 0; -5 0 11] |
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] |
b = [18 22 54 22; 22 70 86 62; 54 86 174 134; 42 62 134 106] |
||
Line 2,087: | Line 2,062: | ||
9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026] |
9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026] |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=scala>// version 1.0.6 |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
fun cholesky(a: DoubleArray): DoubleArray { |
fun cholesky(a: DoubleArray): DoubleArray { |
||
Line 2,142: | Line 2,116: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|Lobster}}== |
=={{header|Lobster}}== |
||
{{trans|Go}} |
{{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. |
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. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lobster">import std |
||
// choleskyLower returns the cholesky decomposition of a symmetric real |
// choleskyLower returns the cholesky decomposition of a symmetric real |
||
Line 2,247: | Line 2,220: | ||
9.899494936612 1.624553864214 1.849711005231 1.392621247646 |
9.899494936612 1.624553864214 1.849711005231 1.392621247646 |
||
</pre> |
</pre> |
||
=={{header|Maple}}== |
=={{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. |
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. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="maple">> A := << 25, 15, -5; 15, 18, 0; -5, 0, 11 >>; |
||
[25 15 -5] |
[25 15 -5] |
||
[ ] |
[ ] |
||
Line 2,302: | Line 2,274: | ||
[ ] |
[ ] |
||
[9.899494934 1.624553864 1.849711006 1.392621248]</syntaxhighlight> |
[9.899494934 1.624553864 1.849711006 1.392621248]</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">CholeskyDecomposition[{{25, 15, -5}, {15, 18, 0}, {-5, 0, 11}}]</syntaxhighlight> |
||
Without the use of built-in functions, making use of memoization: |
Without the use of built-in functions, making use of memoization: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">chol[A_] := |
||
Module[{L}, |
Module[{L}, |
||
L[k_, k_] := L[k, k] = Sqrt[A[[k, k]] - Sum[L[k, j]^2, {j, 1, k-1}]]; |
L[k_, k_] := L[k, k] = Sqrt[A[[k, k]] - Sum[L[k, j]^2, {j, 1, k-1}]]; |
||
Line 2,312: | Line 2,283: | ||
PadRight[Table[L[i, j], {i, Length[A]}, {j, i}]] |
PadRight[Table[L[i, j], {i, Length[A]}, {j, i}]] |
||
]</syntaxhighlight> |
]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
The cholesky decomposition chol() is an internal function |
The cholesky decomposition chol() is an internal function |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="matlab"> A = [ |
||
25 15 -5 |
25 15 -5 |
||
15 18 0 |
15 18 0 |
||
Line 2,344: | Line 2,314: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
<syntaxhighlight lang=maxima>/* Cholesky decomposition is built-in */ |
<syntaxhighlight lang="maxima">/* Cholesky decomposition is built-in */ |
||
a: hilbert_matrix(4)$ |
a: hilbert_matrix(4)$ |
||
Line 2,358: | Line 2,327: | ||
b . transpose(b) - a; |
b . transpose(b) - a; |
||
matrix([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0])</syntaxhighlight> |
matrix([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0])</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=nim>import math, strutils, strformat |
<syntaxhighlight lang="nim">import math, strutils, strformat |
||
type Matrix[N: static int, T: SomeFloat] = array[N, array[N, T]] |
type Matrix[N: static int, T: SomeFloat] = array[N, array[N, T]] |
||
Line 2,403: | Line 2,371: | ||
12.72792 3.04604 1.64974 0.00000 |
12.72792 3.04604 1.64974 0.00000 |
||
9.89949 1.62455 1.84971 1.39262</pre> |
9.89949 1.62455 1.84971 1.39262</pre> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=objeck> |
<syntaxhighlight lang="objeck"> |
||
class Cholesky { |
class Cholesky { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 2,463: | Line 2,430: | ||
9.89949494 1.62455386 1.84971101 1.39262125 |
9.89949494 1.62455386 1.84971101 1.39262125 |
||
</pre> |
</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ocaml">let cholesky inp = |
||
let n = Array.length inp in |
let n = Array.length inp in |
||
let res = Array.make_matrix n n 0.0 in |
let res = Array.make_matrix n n 0.0 in |
||
Line 2,515: | Line 2,481: | ||
12.72792 3.04604 1.64974 0.00000 |
12.72792 3.04604 1.64974 0.00000 |
||
9.89949 1.62455 1.84971 1.39262</pre> |
9.89949 1.62455 1.84971 1.39262</pre> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
{{trans|REXX}} |
{{trans|REXX}} |
||
<syntaxhighlight lang=oorexx>/*REXX program performs the Cholesky decomposition on a square matrix. */ |
<syntaxhighlight lang="oorexx">/*REXX program performs the Cholesky decomposition on a square matrix. */ |
||
niner = '25 15 -5' , /*define a 3x3 matrix. */ |
niner = '25 15 -5' , /*define a 3x3 matrix. */ |
||
'15 18 0' , |
'15 18 0' , |
||
Line 2,569: | Line 2,534: | ||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/ |
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.*/</syntaxhighlight> |
numeric digits d; return (g/1)i /*make complex if X < 0.*/</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<syntaxhighlight lang=parigp>cholesky(M) = |
<syntaxhighlight lang="parigp">cholesky(M) = |
||
{ |
{ |
||
my (L = matrix(#M,#M)); |
my (L = matrix(#M,#M)); |
||
Line 2,605: | Line 2,569: | ||
[9.8995 1.6246 1.8497 1.3926] |
[9.8995 1.6246 1.8497 1.3926] |
||
</pre> |
</pre> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<syntaxhighlight lang=pascal>program CholeskyApp; |
<syntaxhighlight lang="pascal">program CholeskyApp; |
||
type |
type |
||
Line 2,686: | Line 2,649: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang=perl>sub cholesky { |
<syntaxhighlight lang="perl">sub cholesky { |
||
my $matrix = shift; |
my $matrix = shift; |
||
my $chol = [ map { [(0) x @$matrix ] } @$matrix ]; |
my $chol = [ map { [(0) x @$matrix ] } @$matrix ]; |
||
Line 2,727: | Line 2,689: | ||
9.8995 1.6246 1.8497 1.3926 |
9.8995 1.6246 1.8497 1.3926 |
||
</pre> |
</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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,766: | Line 2,727: | ||
{9.899494937,1.624553864,1.849711005,1.392621248}} |
{9.899494937,1.624553864,1.849711005,1.392621248}} |
||
</pre> |
</pre> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(scl 9) |
||
(load "@lib/math.l") |
(load "@lib/math.l") |
||
Line 2,786: | Line 2,746: | ||
(prinl) ) ) )</syntaxhighlight> |
(prinl) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(cholesky |
||
'((25.0 15.0 -5.0) (15.0 18.0 0) (-5.0 0 11.0)) ) |
'((25.0 15.0 -5.0) (15.0 18.0 0) (-5.0 0 11.0)) ) |
||
Line 2,806: | Line 2,766: | ||
12.72792 3.04604 1.64974 0.00000 |
12.72792 3.04604 1.64974 0.00000 |
||
9.89949 1.62455 1.84971 1.39262</pre> |
9.89949 1.62455 1.84971 1.39262</pre> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="pl/i">(subscriptrange): |
||
decompose: procedure options (main); /* 31 October 2013 */ |
decompose: procedure options (main); /* 31 October 2013 */ |
||
declare a(*,*) float controlled; |
declare a(*,*) float controlled; |
||
Line 2,875: | Line 2,834: | ||
9.89950 1.62455 1.84971 1.39262 |
9.89950 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="powershell"> |
||
function cholesky ($a) { |
function cholesky ($a) { |
||
$l = @() |
$l = @() |
||
Line 2,954: | Line 2,912: | ||
9.89949493661167 1.62455386421379 1.84971100523138 1.39262124764559 |
9.89949493661167 1.62455386421379 1.84971100523138 1.39262124764559 |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python2.X version=== |
===Python2.X version=== |
||
<syntaxhighlight lang=python>from __future__ import print_function |
<syntaxhighlight lang="python">from __future__ import print_function |
||
from pprint import pprint |
from pprint import pprint |
||
Line 2,997: | Line 2,954: | ||
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>. |
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>. |
||
<syntaxhighlight lang=python>def cholesky(A): |
<syntaxhighlight lang="python">def cholesky(A): |
||
L = [[0.0] * len(A) for _ in range(len(A))] |
L = [[0.0] * len(A) for _ in range(len(A))] |
||
for i, (Ai, Li) in enumerate(zip(A, L)): |
for i, (Ai, Li) in enumerate(zip(A, L)): |
||
Line 3,008: | Line 2,965: | ||
{{out}} |
{{out}} |
||
(As above) |
(As above) |
||
=={{header|q}}== |
=={{header|q}}== |
||
<syntaxhighlight lang=q>solve:{[A;B] $[0h>type A;B%A;inv[A] mmu B]} |
<syntaxhighlight lang="q">solve:{[A;B] $[0h>type A;B%A;inv[A] mmu B]} |
||
ak:{[m;k] (),/:m[;k]til k:k-1} |
ak:{[m;k] (),/:m[;k]til k:k-1} |
||
akk:{[m;k] m[k;k:k-1]} |
akk:{[m;k] m[k;k:k-1]} |
||
Line 3,038: | Line 2,994: | ||
9.899495 1.624554 1.849711 1.392621 |
9.899495 1.624554 1.849711 1.392621 |
||
</pre> |
</pre> |
||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang=r>t(chol(matrix(c(25, 15, -5, 15, 18, 0, -5, 0, 11), nrow=3, ncol=3))) |
<syntaxhighlight lang="r">t(chol(matrix(c(25, 15, -5, 15, 18, 0, -5, 0, 11), nrow=3, ncol=3))) |
||
# [,1] [,2] [,3] |
# [,1] [,2] [,3] |
||
# [1,] 5 0 0 |
# [1,] 5 0 0 |
||
Line 3,052: | Line 3,007: | ||
# [3,] 12.727922 3.046038 1.649742 0.000000 |
# [3,] 12.727922 3.046038 1.649742 0.000000 |
||
# [4,] 9.899495 1.624554 1.849711 1.392621</syntaxhighlight> |
# [4,] 9.899495 1.624554 1.849711 1.392621</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket> |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require math) |
(require math) |
||
Line 3,085: | Line 3,039: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
Output: |
Output: |
||
<syntaxhighlight lang=racket> |
<syntaxhighlight lang="racket"> |
||
'#(#(5 0 0) |
'#(#(5 0 0) |
||
#(3 3 0) |
#(3 3 0) |
||
Line 3,095: | Line 3,049: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
Line 3,132: | Line 3,085: | ||
12.728 3.046 1.650 0.000 |
12.728 3.046 1.650 0.000 |
||
9.899 1.625 1.850 1.393</pre> |
9.899 1.625 1.850 1.393</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
If trailing zeros are wanted after the decimal point for values of zero (0), the <big><big>'''/ 1'''</big></big> (a division by unity performs |
If trailing zeros are wanted after the decimal point for values of zero (0), the <big><big>'''/ 1'''</big></big> (a division by unity performs |
||
<br>REXX number normalization) can be removed from the line (number 40) which contains the source statement: |
<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> |
::::: <b> z=z right( format(@.row.col, , dPlaces) / 1, width) </b> |
||
<syntaxhighlight lang=rexx>/*REXX program performs the Cholesky decomposition on a square matrix & displays results*/ |
<syntaxhighlight lang="rexx">/*REXX program performs the Cholesky decomposition on a square matrix & displays results*/ |
||
niner = '25 15 -5' , /*define a 3x3 matrix with elements. */ |
niner = '25 15 -5' , /*define a 3x3 matrix with elements. */ |
||
'15 18 0' , |
'15 18 0' , |
||
Line 3,216: | Line 3,168: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
# Project : Cholesky decomposition |
# Project : Cholesky decomposition |
||
Line 3,273: | Line 3,224: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=ruby>require 'matrix' |
<syntaxhighlight lang="ruby">require 'matrix' |
||
class Matrix |
class Matrix |
||
Line 3,321: | Line 3,271: | ||
[9.899494936611665, 1.6245538642137891, 1.849711005231382, 1.3926212476455924]] |
[9.899494936611665, 1.6245538642137891, 1.849711005231382, 1.3926212476455924]] |
||
</pre> |
</pre> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=rust>fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> { |
<syntaxhighlight lang="rust">fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> { |
||
let mut res = vec![0.0; mat.len()]; |
let mut res = vec![0.0; mat.len()]; |
||
for i in 0..n { |
for i in 0..n { |
||
Line 3,378: | Line 3,327: | ||
9.8995 1.6246 1.8497 1.3926 |
9.8995 1.6246 1.8497 1.3926 |
||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<syntaxhighlight lang=scala>case class Matrix( val matrix:Array[Array[Double]] ) { |
<syntaxhighlight lang="scala">case class Matrix( val matrix:Array[Array[Double]] ) { |
||
// Assuming matrix is positive-definite, symmetric and not empty... |
// Assuming matrix is positive-definite, symmetric and not empty... |
||
Line 3,441: | Line 3,389: | ||
assert(math.round( result * 100000 ) * 0.00001 == math.round( test * 100000 ) * 0.00001) |
assert(math.round( result * 100000 ) * 0.00001 == math.round( test * 100000 ) * 0.00001) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Scilab}}== |
=={{header|Scilab}}== |
||
The Cholesky decomposition is builtin, and an upper triangular matrix is returned, such that $A=L^TL$. |
The Cholesky decomposition is builtin, and an upper triangular matrix is returned, such that $A=L^TL$. |
||
<syntaxhighlight lang=scilab>a = [25 15 -5; 15 18 0; -5 0 11]; |
<syntaxhighlight lang="scilab">a = [25 15 -5; 15 18 0; -5 0 11]; |
||
chol(a) |
chol(a) |
||
ans = |
ans = |
||
Line 3,465: | Line 3,412: | ||
0. 0. 1.6497422 1.849711 |
0. 0. 1.6497422 1.849711 |
||
0. 0. 0. 1.3926212</syntaxhighlight> |
0. 0. 0. 1.3926212</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
include "math.s7i"; |
include "math.s7i"; |
||
Line 3,539: | Line 3,485: | ||
9.89950 1.62455 1.84971 1.39262 |
9.89950 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
<syntaxhighlight lang=ruby>func cholesky(matrix) { |
<syntaxhighlight lang="ruby">func cholesky(matrix) { |
||
var chol = matrix.len.of { matrix.len.of(0) } |
var chol = matrix.len.of { matrix.len.of(0) } |
||
for row in ^matrix { |
for row in ^matrix { |
||
Line 3,557: | Line 3,502: | ||
Examples: |
Examples: |
||
<syntaxhighlight lang=ruby>var example1 = [ [ 25, 15, -5 ], |
<syntaxhighlight lang="ruby">var example1 = [ [ 25, 15, -5 ], |
||
[ 15, 18, 0 ], |
[ 15, 18, 0 ], |
||
[ -5, 0, 11 ] ]; |
[ -5, 0, 11 ] ]; |
||
Line 3,588: | Line 3,533: | ||
9.8995 1.6246 1.8497 1.3926 |
9.8995 1.6246 1.8497 1.3926 |
||
</pre> |
</pre> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="smalltalk"> |
||
FloatMatrix>>#cholesky |
FloatMatrix>>#cholesky |
||
| l | |
| l | |
||
Line 3,614: | Line 3,558: | ||
^l |
^l |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
See [http://www.stata.com/help.cgi?mf_cholesky Cholesky square-root decomposition] in Stata help. |
See [http://www.stata.com/help.cgi?mf_cholesky Cholesky square-root decomposition] in Stata help. |
||
<syntaxhighlight lang=stata>mata |
<syntaxhighlight lang="stata">mata |
||
: a=25,15,-5\15,18,0\-5,0,11 |
: a=25,15,-5\15,18,0\-5,0,11 |
||
Line 3,657: | Line 3,600: | ||
4 | 9.899494937 1.624553864 1.849711005 1.392621248 | |
4 | 9.899494937 1.624553864 1.849711005 1.392621248 | |
||
+---------------------------------------------------------+</syntaxhighlight> |
+---------------------------------------------------------+</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|Rust}} |
{{trans|Rust}} |
||
<syntaxhighlight lang=swift>func cholesky(matrix: [Double], n: Int) -> [Double] { |
<syntaxhighlight lang="swift">func cholesky(matrix: [Double], n: Int) -> [Double] { |
||
var res = [Double](repeating: 0, count: matrix.count) |
var res = [Double](repeating: 0, count: matrix.count) |
||
Line 3,721: | Line 3,663: | ||
12.727922061357857 3.0460384954008553 1.6497422479090704 0.0 |
12.727922061357857 3.0460384954008553 1.6497422479090704 0.0 |
||
9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026</pre> |
9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026</pre> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=tcl>proc cholesky a { |
<syntaxhighlight lang="tcl">proc cholesky a { |
||
set m [llength $a] |
set m [llength $a] |
||
set n [llength [lindex $a 0]] |
set n [llength [lindex $a 0]] |
||
Line 3,744: | Line 3,685: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Demonstration code: |
Demonstration code: |
||
<syntaxhighlight lang=tcl>set test1 { |
<syntaxhighlight lang="tcl">set test1 { |
||
{25 15 -5} |
{25 15 -5} |
||
{15 18 0} |
{15 18 0} |
||
Line 3,762: | Line 3,703: | ||
{4.242640687119285 0.0 0.0 0.0} {5.185449728701349 6.565905201197403 0.0 0.0} {12.727922061357857 3.0460384954008553 1.6497422479090704 0.0} {9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026} |
{4.242640687119285 0.0 0.0 0.0} {5.185449728701349 6.565905201197403 0.0 0.0} {12.727922061357857 3.0460384954008553 1.6497422479090704 0.0} {9.899494936611667 1.624553864213788 1.8497110052313648 1.3926212476456026} |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
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. |
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. |
||
<syntaxhighlight lang=vb>Function Cholesky(Mat As Range) As Variant |
<syntaxhighlight lang="vb">Function Cholesky(Mat As Range) As Variant |
||
Dim A() As Double, L() As Double, sum As Double, sum2 As Double |
Dim A() As Double, L() As Double, sum As Double, sum2 As Double |
||
Line 3,819: | Line 3,759: | ||
End Function |
End Function |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
<syntaxhighlight lang=vlang>import math |
<syntaxhighlight lang="vlang">import math |
||
// Symmetric and Lower use a packed representation that stores only |
// Symmetric and Lower use a packed representation that stores only |
||
Line 3,943: | Line 3,882: | ||
9.89949 1.62455 1.84971 1.3926 |
9.89949 1.62455 1.84971 1.3926 |
||
</pre> |
</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-matrix}} |
{{libheader|Wren-matrix}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang=ecmascript>import "/matrix" for Matrix |
<syntaxhighlight lang="ecmascript">import "/matrix" for Matrix |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 3,993: | Line 3,931: | ||
| 9.89949 1.62455 1.84971 1.39262| |
| 9.89949 1.62455 1.84971 1.39262| |
||
</pre> |
</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Using the GNU Scientific Library: |
Using the GNU Scientific Library: |
||
<syntaxhighlight lang=zkl>var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library) |
<syntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library) |
||
fcn lowerCholesky(m){ // trans: C |
fcn lowerCholesky(m){ // trans: C |
||
rows:=m.rows; |
rows:=m.rows; |
||
Line 4,032: | Line 3,969: | ||
Or, using lists: |
Or, using lists: |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=zkl>fcn cholesky(mat){ |
<syntaxhighlight lang="zkl">fcn cholesky(mat){ |
||
rows:=mat.len(); |
rows:=mat.len(); |
||
r:=(0).pump(rows,List().write, (0).pump(rows,List,0.0).copy); // matrix of zeros |
r:=(0).pump(rows,List().write, (0).pump(rows,List,0.0).copy); // matrix of zeros |
||
Line 4,042: | Line 3,979: | ||
r |
r |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
<syntaxhighlight lang=zkl>ex1:=L( L(25.0,15.0,-5.0), L(15.0,18.0,0.0), L(-5.0,0.0,11.0) ); |
<syntaxhighlight lang="zkl">ex1:=L( L(25.0,15.0,-5.0), L(15.0,18.0,0.0), L(-5.0,0.0,11.0) ); |
||
printM(cholesky(ex1)); |
printM(cholesky(ex1)); |
||
println("-----------------"); |
println("-----------------"); |
||
Line 4,050: | Line 3,987: | ||
L(42.0, 62.0, 134.0, 106.0,) ); |
L(42.0, 62.0, 134.0, 106.0,) ); |
||
printM(cholesky(ex2));</syntaxhighlight> |
printM(cholesky(ex2));</syntaxhighlight> |
||
<syntaxhighlight lang=zkl>fcn printM(m){ m.pump(Console.println,rowFmt) } |
<syntaxhighlight lang="zkl">fcn printM(m){ m.pump(Console.println,rowFmt) } |
||
fcn rowFmt(row){ ("%9.5f "*row.len()).fmt(row.xplode()) }</syntaxhighlight> |
fcn rowFmt(row){ ("%9.5f "*row.len()).fmt(row.xplode()) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,063: | Line 4,000: | ||
9.89949 1.62455 1.84971 1.39262 |
9.89949 1.62455 1.84971 1.39262 |
||
</pre> |
</pre> |
||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
{{trans|BBC_BASIC}} |
{{trans|BBC_BASIC}} |
||
<syntaxhighlight lang=zxbasic>10 LET d=2000: GO SUB 1000: GO SUB 4000: GO SUB 5000 |
<syntaxhighlight lang="zxbasic">10 LET d=2000: GO SUB 1000: GO SUB 4000: GO SUB 5000 |
||
20 LET d=3000: GO SUB 1000: GO SUB 4000: GO SUB 5000 |
20 LET d=3000: GO SUB 1000: GO SUB 4000: GO SUB 5000 |
||
30 STOP |
30 STOP |