Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Documentation for this module may be created at Module:MatrixFields/doc

local exports = {}

local function trim(str)
	return str:gsub("^%s*", ""):gsub("%s*$", "")
end

local anchor_template = [[<span id="field_$name"></span>]]
local name_template = [[<code>$name</code>]]

local template = trim [=[
=== Column fields $colanchors ===

Column fields in the form <code>c''N''</code> ($colnames) read the values in the specified column as a {{type|Vector$T}}.
The field produces the same result as an equivalent call to [[#getColumn|<code>getColumn(n)</code>]].

Column fields are also available by indexing by the column number alone (i.e. <code>mat[1]</code>.)

=== Row fields $rowanchors ===

Row fields in the form <code>r''N''</code> ($rownames) read the values in the specified row as a {{type|Vector$T}}.
The field produces the same result as an equivalent call to [[#getRow|<code>getRow(n)</code>]].

=== Value fields $valueanchors ===

Value fields in the form <code>v''RC''</code> ($valuenames) read the value (a {{type|number}}) at the specified row and column.
The row number comes first, then the column number.
]=]

function exports.run(frame)
	local input_args = frame.args
	local object_size = input_args.size or error "No size provided (size=)"
	object_size = tonumber(object_size) or error "Size provided was not a number"
	
	local vars = {
		T = tostring(object_size),
		colanchors = {},
		colnames = {},
		rowanchors = {},
		rownames = {},
		valueanchors = {},
		valuenames = {}
	}
	for i = 1, object_size do
		local fn = "c" .. i
		vars.colanchors[i] = anchor_template:gsub("$name", fn)
		vars.colnames[i] = name_template:gsub("$name", fn)
	end
	for i = 1, object_size do
		local fn = "r" .. i
		vars.rowanchors[i] = anchor_template:gsub("$name", fn)
		vars.rownames[i] = name_template:gsub("$name", fn)
	end
	local i = 0
	for x = 1, object_size do
		for y = 1, object_size do
			local fn = "v" .. y .. x
			i = i + 1
			vars.valueanchors[i] = anchor_template:gsub("$name", fn)
			vars.valuenames[i] = name_template:gsub("$name", fn)
		end
	end
	
	local result = template
	for k, v in pairs(vars) do
		if k:find("anchors$") then v = table.concat(v, "") end
		if k:find("names$") then v = table.concat(v, ", ") end
		result = result:gsub("$"..k, v)
	end
	return frame:preprocess(result)
end

return exports