summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/lua-physical/physical-unit.lua
blob: 5c20722fe15c8d993ea1771fc6f801c5c2a1fdeb (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
--[[
This file contains the unit class. It task is keeping 
track of the unit terms.

Copyright (c) 2021 Thomas Jenni

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--

local prefix = ... and (...):match '(.-%.?)[^%.]+$' or ''

-- Unit class
local Unit = {}
Unit.__index = Unit

-- make Unit callable
setmetatable(Unit, {
	__call = function(class, ...)
		return Unit.new(...)
	end
})

-- constructor
function Unit.new(...)
	local arg = {...}

	local u = {}
	setmetatable(u, Unit)

	-- Unit.new()
	if #arg == 0 then
		u._term = {{},{}}
		u.prefixsymbol = nil
		u.prefixname = nil

	-- copy constructor
	elseif #arg == 1 then
		local o = arg[1]

		-- check if given object is a unit
		if getmetatable(o) ~= Unit then
			error("Error: Cannot create a copy of the object, because it's not a unit.")
		end

		u.prefixsymbol = o.prefixsymbol
		u.prefixname = o.prefixname
		u._term = o._term

	-- Unit.new(symbol, name)
	elseif #arg == 2 then
		u.symbol = arg[1]
		u.name = arg[2]
		u._term = {{{u,1}},{}}

	-- Unit.new(symbol, name, prefixsymbol, prefixname)
	elseif #arg == 4 then
		u.symbol = arg[1]
		u.name = arg[2]
		u.prefixsymbol = arg[3]
		u.prefixname = arg[4]
		u._term = {{{u,1}},{}}

	else
		error("Wrong number of arguments. Cannot create the unit.")

	end

	return u
end

function Unit.__mul(o1,o2)
	local u = Unit.new()

	u:_append(o1)
	u:_append(o2)

	return u
end

function Unit.__div(o1,o2)
	local u = Unit.new()

	u:_append(o1)
	u:_append(o2, true)

	return u
end

function Unit.__pow(o,n)
	local u = Unit.new()

	-- invert the term
	if n < 0 then
		u:_append(o,true)
		n = -n
	else
		u:_append(o)
	end

	-- multiply term by factor n
	for i=1,2 do
		local part = u._term[i]
		for j=1,#part do
			part[j][2] = part[j][2] * n
		end
	end

	return u
end

--copy units from an other quantity
function Unit:_append(u, inversed)
	
	local s = {1,2}
	if inversed then
		s = {2,1}
	end

	for i=1,2 do
		local part = u._term[s[i]]
		local term = self._term[i]

		for j=1,#part do
			local unit = part[j][1]
			local exponent = part[j][2]

			term[#term + 1] = {unit, exponent}
		end
	end
end

-- convert quantity to a string
function Unit:__tostring()

	-- assemble unit strings
	local u = {{},{}}

	for i=1,2 do
		local units = self._term[i]

		for j=1,#units do
			local unit = units[j]
			local exponent = unit[2]

			if exponent ~= 0 then
				local symbol = unit[1].symbol

				local s = "_"

				if unit[1].prefixsymbol ~= nil then
					s = s..unit[1].prefixsymbol
				end

				if symbol ~= nil then
					s = s..symbol
				end

				if exponent ~= 1 then
					s = s.."^"..exponent
				end

				table.insert(u[i], s)
			end
		end
	end

	local str = ""

	-- write enumerator
	if #u[1] > 0 then
		str = str.." * "..table.concat(u[1], " * ")
	end

	-- write division sign
	if #u[2] > 0 then
		str = str.." / "
	end

	-- write denominator
	if #u[2] > 1 then
		str = str.."("..table.concat(u[2], " * ")..")"
	else
		str = str..table.concat(u[2], " * ")
	end

	return str
end

-- convert the unit to a unit string which can be read from the latex package siunitx.
function Unit:tosiunitx()

	local s = ""

	for i=1,2 do
		local units = self._term[i]

		for j=1,#units do
			local unit = units[j]
			local e = unit[2]

			if e ~= 0 then
				
				if i == 2 then
					s = s.."\\per"
				end

				if unit[1].prefixname ~= nil then
					s = s.."\\"..unit[1].prefixname
				end

				s = s.."\\"..unit[1].name

				if e ~= 1 then
					s = s.."\\tothe{"..e.."}"
				end
			end
		end
	end

	return s
end


return Unit