matrix-power.lua - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/matrix-power.lua
blob: a279e84c666447803d59374ac0ed599d027cd2fe (plain)
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
local bit = require 'bit'
local gsl = require 'gsl'
local check = require 'check'
local band, rshift = bit.band, bit.rshift
local is_integer = check.is_integer
local tonumber = tonumber
local NT = gsl.CblasNoTrans
local function power_raw(r, m, n, e)
 local aux = gsl.gsl_matrix_alloc(n, n)
 local m2 = aux
 local r_init = false
 while e > 0 do
 if band(e, 1) ~= 0 then
 if r_init then
 -- The content of m2 is not needed here.
 -- We compute r <- r * m using m2 as a temporary store
 gsl.gsl_matrix_memcpy(m2, r)
 gsl.gsl_blas_dgemm(NT, NT, 1, m, m2, 0, r)
 else
 gsl.gsl_matrix_memcpy(r, m)
 end
 r_init = true
 end
 e = rshift(e, 1)
 if e > 0 then
 -- compute m2 <- m * m
 gsl.gsl_blas_dgemm(NT, NT, 1, m, m, 0, m2)
 m, m2 = m2, m
 end
 end
 if aux then gsl.gsl_matrix_free(aux) end
end
local function power_complex_raw(r, m, n, e)
 local aux = gsl.gsl_matrix_complex_alloc(n, n)
 local m2 = aux
 local r_init = false
 while e > 0 do
 if band(e, 1) ~= 0 then
 if r_init then
 -- The content of m2 is not needed here.
 -- We compute r <- r * m using m2 as a temporary store
 gsl.gsl_matrix_complex_memcpy(m2, r)
 gsl.gsl_blas_zgemm(NT, NT, 1, m, m2, 0, r)
 else
 gsl.gsl_matrix_complex_memcpy(r, m)
 end
 r_init = true
 end
 e = rshift(e, 1)
 if e > 0 then
 -- compute m2 <- m * m
 gsl.gsl_blas_zgemm(NT, NT, 1, m, m, 0, m2)
 m, m2 = m2, m
 end
 end
 if aux then gsl.gsl_matrix_complex_free(aux) end
end
local function power_check_input(m, e)
 if not is_integer(e) then
 error("exponent should be an integer", 3)
 end
 if m.size1 ~= m.size2 then
 error("cannot compute powers of a non-square matrix", 3)
 end
end
local function matrix_power(m, e)
 power_check_input(m, e)
 local n = tonumber(m.size1)
 local r = matrix.alloc(n, n)
 if e > 0 then
 local aux = gsl.gsl_matrix_alloc(n, n)
 gsl.gsl_matrix_memcpy(aux, m)
 power_raw(r, aux, n, e)
 gsl.gsl_matrix_free(aux)
 elseif e == 0 then
 gsl.gsl_matrix_set_identity(r)
 else
 local mi = matrix.inv(m)
 power_raw(r, mi, n, -e)
 end
 return r
end
local function matrix_complex_power(m, e)
 power_check_input(m, e)
 local n = tonumber(m.size1)
 local r = matrix.calloc(n, n)
 if e > 0 then
 local aux = gsl.gsl_matrix_complex_alloc(n, n)
 gsl.gsl_matrix_complex_memcpy(aux, m)
 power_complex_raw(r, aux, n, e)
 gsl.gsl_matrix_complex_free(aux)
 elseif e == 0 then
 gsl.gsl_matrix_complex_set_identity(r)
 else
 local mi = matrix.inv(m)
 power_complex_raw(r, mi, n, -e)
 end
 return r
end
return {power = matrix_power, cpower = matrix_complex_power}
generated by cgit v1.2.3 (git 2.25.1) at 2025年09月10日 20:45:38 +0000

AltStyle によって変換されたページ (->オリジナル) /