Module:Temperament data: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
CompactStar (talk | contribs)
No edit summary
CompactStar (talk | contribs)
https://aalexan3.math.ncsu.edu/articles/mat-inv-rep.pdf
Tags: Mobile edit Mobile web edit
Line 67: Line 67:
end
end


local function matinv(a)
function p.matinv(a)
xk = {}
dbl_identity = {}
for i = 1, #a do
for i = 1, #a do
xk[i] = {}
dbl_identity[i] = {}
for j = 1, #(a[1]) do
for j = 1, #a do
xk[i][j] = {math.random(), math.random()}
if i == j then
dbl_identity[i][j] = {2, 0}
else
dbl_identity[i][j] = {0, 0}
end
end
end
end
end
for i = 1, 15 do
xk = matsub(scalarmatmul({2,0}, xk), matmul(xk, axk))
xn = scalarmatmul(a, {0.0001, 0})
for i = 1, 30 do
xn = matmul(xn, matsub(dbl_identity, matmul(a, xn)))
end
end
return xk
return xn
end
end


return p
return p

Revision as of 07:31, 15 October 2023

Module documentation[view] [edit] [history] [purge]
Note: Do not invoke this module directly; use the corresponding template instead: Template:Temperament data.
Todo: add documentation

local rat = require('Module:Rational')
local p = {}

-- Complex number functions (1st element = real and 2nd element = imaginary)
local function cadd(a, b)
	return {(a[1] + b[1]), (a[2] + b[2])}
end

local function csub(a, b)
	return {(a[1] - b[1]), (a[2] - b[2])}
end

local function cmul(a, b)
	return {(a[1] * b[1] - a[2] * b[2]), (a[1] * b[2] + a[2] * b[1])}
end

local function cdiv(a, b)
	return {(a[1] * b[1] + a[2] * b[2])/(b[1]*b[1] + b[2]*b[2]), (a[2] * b[1] - a[1] * b[2])/(b[1]*b[1] + b[2]*b[2])}
end

local function matadd(a, b)
	local result = {}
	for i = 1, #a  do
		result[i] = {}
		for j = 1, #(b[1]) do
			result[i][j] = cadd(a[i][j], b[i][j])
		end
	end
	return result
end

local function matsub(a, b)
	local result = {}
	for i = 1, #a  do
		result[i] = {}
		for j = 1, #(b[1]) do
			result[i][j] = csub(a[i][j], b[i][j])
		end
	end
	return result
end

local function matmul(a, b)
	local result = {}
	for i = 1, #a  do
		result[i] = {}
		for j = 1, #(b[1]) do
			result[i][j] = {0, 0}
			for k = 1, #(a[1]) do
				result[i][j] = cadd(result[i][j], cmul(a[i][k], b[k][j]))
			end
		end
	end
	return result
end


local function scalarmatmul(a, b)
	local result = {}
	for i = 1, #a  do
		result[i] = {}
		for j = 1, #(a[1]) do
			result[i][j] = cmul(a[i][j], b)
		end
	end
	return result
end

function p.matinv(a)
	dbl_identity = {}
	for i = 1, #a do
		dbl_identity[i] = {}
		for j = 1, #a do
			if i == j then
				dbl_identity[i][j] = {2, 0}
			else
				dbl_identity[i][j] = {0, 0}
			end
		end
	end
	
	xn = scalarmatmul(a, {0.0001, 0})
	
	for i = 1, 30 do
		xn = matmul(xn, matsub(dbl_identity, matmul(a, xn)))
	end
	return xn
end

return p