September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,66 @@
|
|||
' version 18-01-2017
|
||||
' compile with: fbc -s console
|
||||
|
||||
Sub Cholesky_decomp(array() As Double)
|
||||
|
||||
Dim As Integer i, j, k
|
||||
Dim As Double s, l(UBound(array), UBound(array, 2))
|
||||
|
||||
For i = 0 To UBound(array)
|
||||
For j = 0 To i
|
||||
s = 0
|
||||
For k = 0 To j -1
|
||||
s += l(i, k) * l(j, k)
|
||||
Next
|
||||
If i = j Then
|
||||
l(i, j) = Sqr(array(i, i) - s)
|
||||
Else
|
||||
l(i, j) = (array(i, j) - s) / l(j, j)
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
|
||||
For i = 0 To UBound(array)
|
||||
For j = 0 To UBound(array, 2)
|
||||
Swap array(i, j), l(i, j)
|
||||
Next
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Sub Print_(array() As Double)
|
||||
|
||||
Dim As Integer i, j
|
||||
|
||||
For i = 0 To UBound(array)
|
||||
For j = 0 To UBound(array, 2)
|
||||
Print Using "###.#####";array(i,j);
|
||||
Next
|
||||
Print
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim m1(2,2) As Double => {{25, 15, -5}, _
|
||||
{15, 18, 0}, _
|
||||
{-5, 0, 11}}
|
||||
|
||||
Dim m2(3, 3) As Double => {{18, 22, 54, 42}, _
|
||||
{22, 70, 86, 62}, _
|
||||
{54, 86, 174, 134}, _
|
||||
{42, 62, 134, 106}}
|
||||
|
||||
Cholesky_decomp(m1())
|
||||
Print_(m1())
|
||||
|
||||
Print
|
||||
Cholesky_decomp(m2())
|
||||
Print_(m2())
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
11
Task/Cholesky-decomposition/J/cholesky-decomposition-3.j
Normal file
11
Task/Cholesky-decomposition/J/cholesky-decomposition-3.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
load 'math/lapack'
|
||||
load 'math/lapack/potrf'
|
||||
potrf_jlapack_ eg1
|
||||
5 0 0
|
||||
3 3 0
|
||||
_1 1 3
|
||||
potrf_jlapack_ eg2
|
||||
4.24264 0 0 0
|
||||
5.18545 6.56591 0 0
|
||||
12.7279 3.04604 1.64974 0
|
||||
9.89949 1.62455 1.84971 1.39262
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun cholesky(a: DoubleArray): DoubleArray {
|
||||
val n = Math.sqrt(a.size.toDouble()).toInt()
|
||||
val l = DoubleArray(a.size)
|
||||
var s: Double
|
||||
for (i in 0 until n)
|
||||
for (j in 0 .. i) {
|
||||
s = 0.0
|
||||
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
|
||||
l[i * n + j] = when {
|
||||
(i == j) -> Math.sqrt(a[i * n + i] - s)
|
||||
else -> 1.0 / l[j * n + j] * (a[i * n + j] - s)
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
fun showMatrix(a: DoubleArray) {
|
||||
val n = Math.sqrt(a.size.toDouble()).toInt()
|
||||
for (i in 0 until n) {
|
||||
for (j in 0 until n) print("%8.5f ".format(a[i * n + j]))
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val m1 = doubleArrayOf(25.0, 15.0, -5.0,
|
||||
15.0, 18.0, 0.0,
|
||||
-5.0, 0.0, 11.0)
|
||||
val c1 = cholesky(m1)
|
||||
showMatrix(c1)
|
||||
println()
|
||||
val m2 = doubleArrayOf(18.0, 22.0, 54.0, 42.0,
|
||||
22.0, 70.0, 86.0, 62.0,
|
||||
54.0, 86.0, 174.0, 134.0,
|
||||
42.0, 62.0, 134.0, 106.0)
|
||||
val c2 = cholesky(m2)
|
||||
showMatrix(c2)
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*REXX program performs the Cholesky decomposition on a square matrix. */
|
||||
niner = '25 15 -5' , /*define a 3x3 matrix. */
|
||||
'15 18 0' ,
|
||||
'-5 0 11'
|
||||
call Cholesky niner
|
||||
hexer = 18 22 54 42, /*define a 4x4 matrix. */
|
||||
22 70 86 62,
|
||||
54 86 174 134,
|
||||
42 62 134 106
|
||||
call Cholesky hexer
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
Cholesky: procedure; parse arg mat; say; say; call tell 'input matrix',mat
|
||||
do r=1 for ord
|
||||
do c=1 for r; d=0; do i=1 for c-1; d=d+!.r.i*!.c.i; end /*i*/
|
||||
if r=c then !.r.r=sqrt(!.r.r-d)
|
||||
else !.r.c=1/!.c.c*(a.r.c-d)
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
call tell 'Cholesky factor',,!.,'-'
|
||||
return
|
||||
/*----------------------------------------------------------------------------*/
|
||||
err: say; say; say '***error***!'; say; say arg(1); say; say; exit 13
|
||||
/*----------------------------------------------------------------------------*/
|
||||
tell: parse arg hdr,x,y,sep; n=0; if sep=='' then sep='-'
|
||||
dPlaces= 5 /*n decimal places past the decimal point*/
|
||||
width =10 /*width of field used to display elements*/
|
||||
if y=='' then !.=0
|
||||
else do row=1 for ord; do col=1 for ord; x=x !.row.col; end; end
|
||||
w=words(x)
|
||||
do ord=1 until ord**2>=w; end /*a fast way to find matrix's order*/
|
||||
say
|
||||
if ord**2\==w then call err "matrix elements don't form a square matrix."
|
||||
say center(hdr, ((width+1)*w)%ord, sep)
|
||||
say
|
||||
do row=1 for ord; z=''
|
||||
do col=1 for ord; n=n+1
|
||||
a.row.col=word(x,n)
|
||||
if col<=row then !.row.col=a.row.col
|
||||
z=z right( format(a.row.col,, dPlaces) / 1, width)
|
||||
end /*col*/
|
||||
say z
|
||||
end /*row*/
|
||||
return
|
||||
/*----------------------------------------------------------------------------*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=''; m.=9
|
||||
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
|
||||
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*/
|
||||
numeric digits d; return (g/1)i /*make complex if X < 0.*/
|
||||
23
Task/Cholesky-decomposition/Phix/cholesky-decomposition.phix
Normal file
23
Task/Cholesky-decomposition/Phix/cholesky-decomposition.phix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function cholesky(sequence matrix)
|
||||
integer l = length(matrix)
|
||||
sequence chol = repeat(repeat(0,l),l)
|
||||
for row=1 to l do
|
||||
for col=1 to row do
|
||||
atom x = matrix[row][col]
|
||||
for i=1 to col do
|
||||
x -= chol[row][i] * chol[col][i]
|
||||
end for
|
||||
chol[row][col] = iff(row == col ? sqrt(x) : x/chol[col][col])
|
||||
end for
|
||||
end for
|
||||
return chol
|
||||
end function
|
||||
|
||||
ppOpt({pp_Nest,1})
|
||||
pp(cholesky({{ 25, 15, -5 },
|
||||
{ 15, 18, 0 },
|
||||
{ -5, 0, 11 }}))
|
||||
pp(cholesky({{ 18, 22, 54, 42},
|
||||
{ 22, 70, 86, 62},
|
||||
{ 54, 86, 174, 134},
|
||||
{ 42, 62, 134, 106}}))
|
||||
16
Task/Cholesky-decomposition/Q/cholesky-decomposition.q
Normal file
16
Task/Cholesky-decomposition/Q/cholesky-decomposition.q
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
solve:{[A;B] $[0h>type A;B%A;inv[A] mmu B]}
|
||||
ak:{[m;k] (),/:m[;k]til k:k-1}
|
||||
akk:{[m;k] m[k;k:k-1]}
|
||||
transpose:{$[0h=type x;flip x;enlist each x]}
|
||||
mult:{[A;B]$[0h=type A;A mmu B;A*B]}
|
||||
cholesky:{[A]
|
||||
{[A;L;n]
|
||||
l_k:solve[L;ak[A;n]];
|
||||
l_kk:first over sqrt[akk[A;n] - mult[transpose l_k;l_k]];
|
||||
({$[0h<type x;enlist x;x]}L,'0f),enlist raze transpose[l_k],l_kk
|
||||
}[A]/[sqrt A[0;0];1_1+til count first A]
|
||||
}
|
||||
|
||||
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)
|
||||
40
Task/Cholesky-decomposition/Rust/cholesky-decomposition.rust
Normal file
40
Task/Cholesky-decomposition/Rust/cholesky-decomposition.rust
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
|
||||
let mut res = vec![0.0; mat.len()];
|
||||
for i in 0..n {
|
||||
for j in 0..(i+1){
|
||||
let mut s = 0.0;
|
||||
for k in 0..j {
|
||||
s += res[i * n + k] * res[j * n + k];
|
||||
}
|
||||
res[i * n + j] = if i == j { (mat[i * n + i] - s).sqrt() } else { (1.0 / res[j * n + j] * (mat[i * n + j] - s)) };
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn show_matrix(matrix: Vec<f64>, n: usize){
|
||||
for i in 0..n {
|
||||
for j in 0..n {
|
||||
print!("{:.4}\t", matrix[i * n + j]);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let dimension = 3 as usize;
|
||||
let m1 = vec![25.0, 15.0, -5.0,
|
||||
15.0, 18.0, 0.0,
|
||||
-5.0, 0.0, 11.0];
|
||||
let res1 = cholesky(m1, dimension);
|
||||
show_matrix(res1, dimension);
|
||||
|
||||
let dimension = 4 as usize;
|
||||
let m2 = vec![18.0, 22.0, 54.0, 42.0,
|
||||
22.0, 70.0, 86.0, 62.0,
|
||||
54.0, 86.0, 174.0, 134.0,
|
||||
42.0, 62.0, 134.0, 106.0];
|
||||
let res2 = cholesky(m2, dimension);
|
||||
show_matrix(res2, dimension);
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
mata
|
||||
: a=25,15,-5\15,18,0\-5,0,11
|
||||
|
||||
: a
|
||||
[symmetric]
|
||||
1 2 3
|
||||
+----------------+
|
||||
1 | 25 |
|
||||
2 | 15 18 |
|
||||
3 | -5 0 11 |
|
||||
+----------------+
|
||||
|
||||
: cholesky(a)
|
||||
1 2 3
|
||||
+----------------+
|
||||
1 | 5 0 0 |
|
||||
2 | 3 3 0 |
|
||||
3 | -1 1 3 |
|
||||
+----------------+
|
||||
|
||||
: a=18,22,54,42\22,70,86,62\54,86,174,134\42,62,134,106
|
||||
|
||||
: a
|
||||
[symmetric]
|
||||
1 2 3 4
|
||||
+-------------------------+
|
||||
1 | 18 |
|
||||
2 | 22 70 |
|
||||
3 | 54 86 174 |
|
||||
4 | 42 62 134 106 |
|
||||
+-------------------------+
|
||||
|
||||
: cholesky(a)
|
||||
1 2 3 4
|
||||
+---------------------------------------------------------+
|
||||
1 | 4.242640687 0 0 0 |
|
||||
2 | 5.185449729 6.565905201 0 0 |
|
||||
3 | 12.72792206 3.046038495 1.649742248 0 |
|
||||
4 | 9.899494937 1.624553864 1.849711005 1.392621248 |
|
||||
+---------------------------------------------------------+
|
||||
11
Task/Cholesky-decomposition/Zkl/cholesky-decomposition-1.zkl
Normal file
11
Task/Cholesky-decomposition/Zkl/cholesky-decomposition-1.zkl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
|
||||
fcn lowerCholesky(m){ // trans: C
|
||||
rows:=m.rows;
|
||||
lcm:=GSL.Matrix(rows,rows); // zero filled
|
||||
foreach i,j in (rows,i+1){
|
||||
s:=(0).reduce(j,'wrap(s,k){ s + lcm[i,k]*lcm[j,k] },0.0);
|
||||
lcm[i,j]=( if(i==j)(m[i,i] - s).sqrt()
|
||||
else 1.0/lcm[j,j]*(m[i,j] - s) );
|
||||
}
|
||||
lcm
|
||||
}
|
||||
10
Task/Cholesky-decomposition/Zkl/cholesky-decomposition-2.zkl
Normal file
10
Task/Cholesky-decomposition/Zkl/cholesky-decomposition-2.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fcn cholesky(mat){
|
||||
rows:=mat.len();
|
||||
r:=(0).pump(rows,List().write, (0).pump(rows,List,0.0).copy); // matrix of zeros
|
||||
foreach i,j in (rows,i+1){
|
||||
s:=(0).reduce(j,'wrap(s,k){ s + r[i][k]*r[j][k] },0.0);
|
||||
r[i][j]=( if(i==j)(mat[i][i] - s).sqrt()
|
||||
else 1.0/r[j][j]*(mat[i][j] - s) );
|
||||
}
|
||||
r
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
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));
|
||||
println("-----------------");
|
||||
ex2:=L( L(18.0, 22.0, 54.0, 42.0,),
|
||||
L(22.0, 70.0, 86.0, 62.0,),
|
||||
L(54.0, 86.0, 174.0, 134.0,),
|
||||
L(42.0, 62.0, 134.0, 106.0,) );
|
||||
printM(cholesky(ex2));
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fcn printM(m){ m.pump(Console.println,rowFmt) }
|
||||
fcn rowFmt(row){ ("%9.5f "*row.len()).fmt(row.xplode()) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue