-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymdiff.lua
791 lines (725 loc) · 23.5 KB
/
symdiff.lua
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
--[[
Copyright © 2023 William Quelho Ferreira
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.
]]
-- TODO make sum and product nodes support more than 2 parents
---@module "symdiff"
local M = {}
M._VERSION = "1.1.0"
-- Configuration region
---@alias sdNumeric number
---Checks if a value is sdNumeric
---@param value any
---@return boolean
local isNumeric = function(value)
return type(value) == "number"
end
---Check if sdNumeric value is zero
---@param value sdNumeric
---@return boolean
local isZero = function(value)
return value == 0
end
local zeroVal = 0
---Check if sdNumeric value is one
---@param value sdNumeric
---@return boolean
local isOne = function(value)
return value == 1
end
local oneVal = 1
---Convert a number to a sdNumeric
---@param value number
---@return sdNumeric
local convert = function(value)
return value
end
-- End of configuration region
---@diagnostic disable-next-line: deprecated
local unpack = unpack or table.unpack
local add, diff, mul, div, pow
---@class Expression
---@operator add(AlgebraicTerm): Expression
---@operator sub(AlgebraicTerm): Expression
---@operator mul(AlgebraicTerm): Expression
---@operator div(AlgebraicTerm): Expression
---@operator pow(AlgebraicTerm): Expression
---@operator unm(): Expression
---@field public derivative DerivativeAccessor
---@field package cachedDerivatives {Variable: Expression}
---@field package name string?
---@field package nodeType string
---@field package eval fun(self: Expression, point: Point): sdNumeric|Expression
---@field package format fun(self): string
---@field package dependencies {Variable: boolean}
---@field package calculateDerivative fun(self: Expression, variable: Variable)
---@field package parents Expression[]
---@field package cachedFormat string?
M.Expression = {}
local Expression__meta = {}
Expression__meta.__index = M.Expression
---@class Variable: Expression
---@operator add(AlgebraicTerm): Expression
---@operator sub(AlgebraicTerm): Expression
---@operator mul(AlgebraicTerm): Expression
---@operator div(AlgebraicTerm): Expression
---@operator pow(AlgebraicTerm): Expression
---@operator unm(): Expression
---@class Function
---@operator call(AlgebraicTerm): Expression
---@field public func WrappedFunction
---@field package funcDerivative Function
---@field package actsOnExpressions boolean?
---@field package repr (string|fun(arg: Expression): string)?
local FuncWrapper = {}
local FuncWrapper__meta = {}
---@class FunctionCall: Expression
---@field package repr (string|fun(arg: Expression): string)?
---@field package funcWrapper table?
---@field package func fun(arg: sdNumeric): sdNumeric
---@field package funcDerivative Function
---@field package actsOnExpressions boolean?
---@alias Point
---|{Variable: sdNumeric} a mapping for the value of each variable at the point
---|number the value of the single Variable of the Expression
---@alias AlgebraicTerm Variable|Expression|sdNumeric
---@alias WrappedFunction (fun(arg: sdNumeric): sdNumeric)|(fun(arg: Expression): Expression)
local zero, one
---@type Point
-- Used for evaluating constants without littering memory space
local nullPoint = {}
---@enum nodeTypes
local nodeTypes = {
const = "constant",
var = "variable",
sum = "sum",
diff = "difference",
unm = "negation",
mul = "product",
div = "division",
pow = "power",
func = "function"
}
setmetatable(nodeTypes, {__index = function(_, k) error("Unknown nodeType: "..tostring(k)) end})
---@enum priorities
local priorities = {
[nodeTypes.func] = 11,
[nodeTypes.const] = 10,
[nodeTypes.var] = 10,
[nodeTypes.unm] = 9,
[nodeTypes.pow] = 8,
[nodeTypes.div] = 3,
[nodeTypes.mul] = 2,
[nodeTypes.diff] = 1,
[nodeTypes.sum] = 1,
}
---Get this Expression's dependency, if is the only dependency
---@param expr Expression
---@return Variable? dep the dependency, if single, nil otherwise
---@return string? reason the reason a single dependency couldn't be fetched
local function getOnlyDependency(expr)
local onlyDep = next(expr.dependencies)
if not onlyDep then
return nil, "expression is constant"
elseif next(expr.dependencies, onlyDep) then
return nil, "multiple dependencies"
else
return onlyDep
end
end
---@class DerivativeAccessor
---@operator call(Variable): Expression
---@operator call(Point): Expression|sdNumeric
---@field expression Expression
local DerivativeAccessor__meta = {}
DerivativeAccessor__meta.__call = function(accessor, expression, arg)
if type(arg) == "table" and arg.nodeType == nodeTypes.var then
if not accessor[arg] then
---@cast arg Variable
if expression.dependencies[arg] then
accessor[arg] = expression:calculateDerivative(arg)
else
accessor[arg] = zero
end
end
return accessor[arg]
else
---@cast arg Point
local onlyDep, reason = getOnlyDependency(expression)
if not onlyDep then
---@cast reason string
if reason:find "constant" then
return zero
elseif reason:find "multiple" then
error("Cannot call derivative without index in expression with multiple dependencies")
else
error("An unknown error has occurred")
end
end
if arg then
return accessor(expression, onlyDep):evaluate(arg)
else
return accessor(expression, onlyDep)
end
end
end
---Get the priority value of a given Expression node
---@param expression Expression
---@return sdNumeric
---@nodiscard
local function getPriority(expression)
return priorities[expression.nodeType]
end
---Create a new derivative accessor for the given Expression
---@return DerivativeAccessor
---@nodiscard
local function createDerivativeAccessor()
local accessor = setmetatable({}, DerivativeAccessor__meta)
return accessor
end
---@param dependent Expression
---@param dependencyVariable Variable
local function addDependency(dependent, dependencyVariable)
dependent.dependencies[dependencyVariable] = true
end
---Merge two maps, prioritizing the new keys
---@param onto table
---@param new table
local function merge(onto, new)
for k, v in pairs(new) do
onto[k] = v
end
end
local function errorIfAnyIsNull(n, ...)
local args = {...}
for idx = 1, n do
if args[idx] == nil then
error("Expected non-nil value for operand #"..idx, 3)
end
end
end
---Check whether expr is a constant node or not
---@param expr Expression the expression to check
---@return boolean const true if and only if expr is a constant node
local function isConstant(expr)
if expr == nil then
error("Unexpected nil value", 2)
end
return expr.nodeType == nodeTypes.const
end
---Checks if the argument is an Expression.
---@param expr any the value to be checked
---@return boolean isExpr whether the value is an expression or not
function M.isExpression(expr)
return getmetatable(expr) == Expression__meta
end
---Wrap parents of the Expression expr in parens if needed
---@param expr Expression the expression being evaluated as a string
---@param parents Expression[] operands of the given Expression
---@return string ... tostring(p) for every p in parents, wrapped with parentheses if necessary
local function wrapParentsIfNeeded(expr, parents)
local results = {}
local ps = getPriority(expr)
for _, p in ipairs(parents) do
local s = tostring(p)
if getPriority(p) < ps then
s = ("(%s)"):format(s)
end
table.insert(results, s)
end
return unpack(results)
end
---Create a new Expression node
---@param nodeType nodeTypes type of the node being created
---@param eval fun(e: Expression, p: Point): Expression|sdNumeric evaluation function based on the Expression's parents
---@param derivative fun(e: Expression, withRespectTo: Variable): Expression evaluation function for the expression's derivative based on its parents
---@param format fun(e: Expression): string function for representing the Expression as a string
---@param parents Expression[] parents of the expression, usually the operands
---@return Expression expr the newly created Expression node
---@nodiscard
local function createBaseExpression(nodeType, eval, derivative, format, parents)
local expr = setmetatable({}, Expression__meta)
expr.nodeType = nodeType
expr.derivative = createDerivativeAccessor()
expr.dependencies = {}
expr.cachedFormat = nil
expr.parents = parents
expr.eval = eval
expr.calculateDerivative = derivative
expr.format = format
for _, parent in ipairs(parents) do
merge(expr.dependencies, parent.dependencies)
end
return expr
end
---Evaluate the Expression at a given point
---@param point Point the point at which to evaluate the Expression
---@return sdNumeric|Expression result a number if there were no unresolved dependencies, an Expression of said dependencies otherwise
function M.Expression:evaluate(point)
if isNumeric(point) then
local onlyDep, reason = getOnlyDependency(self)
---@cast reason string
if onlyDep then
point = {[onlyDep] = point}
elseif reason:find "constant" then
point = nullPoint
elseif reason:find "multiple" then
error("Cannot use implicit variable points when there's multiple dependencies")
else
error("An unknown error has occurred")
end
end
local result = self:eval(point)
if type(result) == "number" then
result = convert(result)
end
return result
end
local function varEval(self, point)
local givenValue = point[self]
if givenValue then
return givenValue
else
return self
end
end
local function varDerivative(self, withRespectTo)
if self == withRespectTo then
return one
else
return zero
end
end
local function varFormat(self)
return self.name
end
---@param name string the name of the variable
---@return Variable
function M.var(name)
assert(type(name) == "string", "Variables must have a name")
---@type Variable
---@diagnostic disable-next-line: assign-type-mismatch
local v = createBaseExpression(
nodeTypes.var,
varEval,
varDerivative,
varFormat,
{}
)
addDependency(v, v)
v.name = name
return v
end
local function createConstEval(value)
return function(_, _)
return value
end
end
local function constDerivative(_, _)
return zero
end
local function constFormat(self)
if self.name then
return self.name
else
return tostring(self:evaluate(nullPoint))
end
end
---Wrap a constant value for use in SymDiff
---@param value any
---@param name string? name of the constant
---@return Expression
function M.const(value, name)
local c = createBaseExpression(
nodeTypes.const,
createConstEval(value),
constDerivative,
constFormat,
{}
)
c.name = name
return c
end
zero = M.const(zeroVal)
one = M.const(oneVal)
local function sumEval(self, point)
return self.parents[1]:evaluate(point) + self.parents[2]:evaluate(point)
end
local function sumDerivative(self, withRespectTo)
return self.parents[1]:derivative(withRespectTo) +
self.parents[2]:derivative(withRespectTo)
end
local function sumFormat(self)
return ("%s + %s"):format(tostring(self.parents[1]), tostring(self.parents[2]))
end
function add(a, b)
errorIfAnyIsNull(2, a, b)
if isNumeric(a) then
a = M.const(a)
end
if isNumeric(b) then
b = M.const(b)
end
if isConstant(b) then
a, b = b, a
end
if isConstant(a) then
local aEval = a:evaluate(nullPoint)
---@cast aEval sdNumeric
if isZero(aEval) then
return b
elseif isConstant(b) then
return M.const(aEval + b:evaluate(nullPoint))
end
end
return createBaseExpression(nodeTypes.sum, sumEval, sumDerivative, sumFormat, {a, b})
end
Expression__meta.__add = function(a, b)
return add(a, b)
end
local function diffEval(self, point)
return self.parents[1]:evaluate(point) - self.parents[2]:evaluate(point)
end
local function diffDerivative(self, withRespectTo)
return self.parents[1]:derivative(withRespectTo) -
self.parents[2]:derivative(withRespectTo)
end
local function diffFormat(self)
return ("%s - %s"):format(tostring(self.parents[1]), tostring(self.parents[2]))
end
function diff(a, b)
errorIfAnyIsNull(2, a, b)
if isNumeric(a) then
a = M.const(a)
end
if isNumeric(b) then
b = M.const(b)
end
if isConstant(a) then
local aEval = a:evaluate(nullPoint)
---@cast aEval sdNumeric
if isZero(aEval) then
return -b
elseif isConstant(b) then
return M.const(aEval - b:evaluate(nullPoint))
end
else
if isConstant(b) and isZero(b:evaluate(nullPoint) --[[@as sdNumeric]]) then
return a
end
end
return createBaseExpression(
nodeTypes.diff,
diffEval,
diffDerivative,
diffFormat,
{a, b}
)
end
Expression__meta.__sub = function(a, b)
return diff(a, b)
end
local function unmEval(self, point)
return -self.parents[1]:evaluate(point)
end
local function unmDerivative(self, withRespectTo)
return -self.parents[1]:derivative(withRespectTo)
end
local function unmFormat(self)
local p1 = wrapParentsIfNeeded(self, self.parents)
return ("-%s"):format(tostring(p1))
end
Expression__meta.__unm = function(a)
if isNumeric(a) then
return M.const(-a)
end
return createBaseExpression(
nodeTypes.unm,
unmEval,
unmDerivative,
unmFormat,
{a}
)
end
local function productEval(self, point)
return self.parents[1]:evaluate(point) * self.parents[2]:evaluate(point)
end
local function constProductDerivative(self, withRespectTo)
return self.parents[1] * self.parents[2]:derivative(withRespectTo)
end
local function productDerivative(self, withRespectTo)
local p1, p2 = self.parents[1], self.parents[2]
return p1*p2:derivative(withRespectTo) + p1:derivative(withRespectTo)*p2
end
local function productFormat(self)
local p1, p2 = wrapParentsIfNeeded(self, self.parents)
return ("%s * %s"):format(p1, p2)
end
local function constProduct(const, expr)
assert(isConstant(const), "First argument to constProduct must be a Constant")
return createBaseExpression(
nodeTypes.mul,
productEval,
constProductDerivative,
productFormat,
{const, expr}
)
end
function mul(a, b)
errorIfAnyIsNull(2, a, b)
if isNumeric(a) then
a = M.const(a)
end
if isNumeric(b) then
b = M.const(b)
end
if isConstant(b) then
a, b = b, a
end
if isConstant(a) then
local aEval = a:evaluate(nullPoint) --[[@as sdNumeric]]
if isOne(aEval) then
return b
elseif isConstant(b) then
return M.const(aEval * b:evaluate(nullPoint))
elseif isZero(aEval) then
return zero
else
return constProduct(a, b)
end
end
return createBaseExpression(
nodeTypes.mul,
productEval,
productDerivative,
productFormat,
{a, b}
)
end
Expression__meta.__mul = function(a, b)
return mul(a, b)
end
local function quotientEval(self, point)
return self.parents[1]:evaluate(point) / self.parents[2]:evaluate(point)
end
local function quotientDerivative(self, withRespectTo)
local a, b = self.parents[1], self.parents[2]
return (a:derivative(withRespectTo) * b - a * b:derivative(withRespectTo)) / (b^2)
end
local function quotientFormat(self)
local p1, p2 = wrapParentsIfNeeded(self, self.parents)
return ("%s / %s"):format(p1, p2)
end
function div(a, b)
errorIfAnyIsNull(2, a, b)
if isNumeric(a) then
a = M.const(a)
end
if isNumeric(b) then
assert(not isZero(b), "Division by 0")
return (1/b) * a
elseif isConstant(b) then
local bEval = b:evaluate(nullPoint)
assert(not isZero(bEval), "Division by 0")
if isConstant(a) then
return M.const(a:evaluate(nullPoint) / bEval)
else
return (1/bEval) * a
end
end
return createBaseExpression(
nodeTypes.div,
quotientEval,
quotientDerivative,
quotientFormat,
{a, b}
)
end
Expression__meta.__div = function(a, b)
return div(a, b)
end
local function powerEval(self, point)
return self.parents[1]:evaluate(point) ^ self.parents[2]:evaluate(point)
end
local function powerRuleDerivative(self, withRespectTo)
assert(isConstant(self.parents[2]), "Power rule only works for constant exponents")
local result = self.parents[2] * self.parents[1] ^ (self.parents[2] - 1) * self.parents[1]:derivative(withRespectTo)
return result
end
local function constantBasePowerDerivative(self, withRespectTo)
assert(isConstant(self.parents[1]), "Constant base power derivative requires constant base")
local result = M.ln(self.parents[1]) *
(self.parents[1]^self.parents[2]) *
self.parents[2]:derivative(withRespectTo)
return result
end
local function generalPowerDerivative(self, withRespectTo)
-- d/dx (f(x)^g(x)) =
-- f(x)^g(x) * (g'(x)ln(f(x)) + (f'(x)g(x))/f(x))
local result = (self.parents[1] ^ self.parents[2]) *
(
self.parents[2]:derivative(withRespectTo)*M.ln(self.parents[1]) +
(self.parents[1]:derivative(withRespectTo)*self.parents[2]) / self.parents[1]
)
return result
end
local function powerFormat(self)
local p1, p2 = wrapParentsIfNeeded(self, self.parents)
return ("%s^%s"):format(p1, p2)
end
function pow(a, b)
errorIfAnyIsNull(2, a, b)
if isNumeric(a) then
a = M.const(a)
end
if isNumeric(b) then
b = M.const(b)
end
local calculateDerivative
if isConstant(b) then
if isZero(b:evaluate(nullPoint) --[[@as sdNumeric]]) then
return one
elseif isOne(b:evaluate(nullPoint) --[[@as sdNumeric]]) then
return a
end
calculateDerivative = powerRuleDerivative
elseif isConstant(a) then
if isZero(a:evaluate(nullPoint) --[[@as sdNumeric]]) then
return zero
end
calculateDerivative = constantBasePowerDerivative
else
calculateDerivative = generalPowerDerivative
end
return createBaseExpression(
nodeTypes.pow,
powerEval,
calculateDerivative,
powerFormat,
{a, b}
)
end
Expression__meta.__pow = function(a, b)
return pow(a, b)
end
local function funcEval(self, point)
errorIfAnyIsNull(1, point)
if self.actsOnExpressions then
return self.func(self.parents[1]):evaluate(point)
else
local arg = self.parents[1]:evaluate(point)
if isNumeric(arg) then
return self.func(arg)
else
return self.funcWrapper(arg)
end
end
end
local function funcDerivative(self, withRespectTo)
local selfDeriv
if self.funcDerivative then
selfDeriv = self.funcDerivative(self.parents[1])
elseif self.actsOnExpressions then
selfDeriv = self.func(self.parents[1]):derivative(withRespectTo)
else
error("No known method for computing derivative of user-specified function")
end
return self.parents[1]:derivative(withRespectTo) * selfDeriv
end
local function funcFormat(self)
if type(self.repr) == "string" then
return ("%s(%s)"):format(self.repr, tostring(self.parents[1]))
elseif self.repr then
return self.repr(self.parents[1])
elseif self.actsOnExpressions then
return tostring(self.func(self.parents[1]))
else
error("Function passed nil to repr argument without specifying actsOnExpressions: cannot be displayed")
end
end
Expression__meta.__tostring = function(self)
if not self.cachedFormat then
self.cachedFormat = self:format()
end
return self.cachedFormat
end
FuncWrapper__meta.__index = FuncWrapper
FuncWrapper__meta.__call = function(self, arg)
if not arg then
error("Function must have exactly 1 argument", 2)
end
if isNumeric(arg) then
arg = M.const(arg)
end
---@type FunctionCall
---@diagnostic disable-next-line: assign-type-mismatch
local f = createBaseExpression(nodeTypes.func, funcEval, funcDerivative, funcFormat, {arg})
f.actsOnExpressions = self.actsOnExpressions
f.repr = self.repr
f.func = self.func
f.funcWrapper = self
f.funcDerivative = self.funcDerivative
return f
end
---@param eval WrappedFunction the function to wrap
---@param numeric boolean? whether the function deals with numbers (true) or Expressions (false, default)
---@param repr (string|fun(Expression): string)? name or formatting function for this Function
---@return Function
function M.func(eval, numeric, repr)
local wrapper = setmetatable({}, FuncWrapper__meta)
wrapper.func = eval
wrapper.actsOnExpressions = not numeric
wrapper.repr = repr
return wrapper
end
---Set the derivative calculating Function for this Function.
---This is recommended only for Functions flagged as numeric.
---@param deriv Function the function whose evaluation equals self's derivative
function FuncWrapper:setDerivative(deriv)
self.funcDerivative = deriv
end
M.identity = M.func(function(x) return x end)
M.reciproc = M.func(function(x) return 1/x end)
M.sqrt = M.func(function(x) return math.sqrt(x --[[@as sdNumeric]]) end, true, "sqrt")
local sqrtDeriv = M.func(function(x) return 1/(2*M.sqrt(x)) end)
M.sqrt:setDerivative(sqrtDeriv)
M.ln = M.func(math.log, true, "ln")
M.ln:setDerivative(M.reciproc)
M.exp = M.func(math.exp, true, "exp")
M.exp:setDerivative(M.exp)
local sinhF = function(x) return (M.exp(x) - M.exp(-x)) / 2 end
M.sinh = M.func(sinhF, false, "sinh")
local coshF = function(x) return (M.exp(x) + M.exp(-x) / 2) end
M.cosh = M.func(coshF, false, "cosh")
local tanhF = function(x) return (M.exp(2*x) - 1) / (M.exp(2*x) + 1) end
M.tanh = M.func(tanhF, false, "tanh")
local tanhDeriv = M.func(function(x) return 1/M.cosh(x)^2 end, true)
M.tanh:setDerivative(tanhDeriv)
M.sinh:setDerivative(M.cosh)
M.cosh:setDerivative(M.sinh)
M.sin = M.func(math.sin, true, "sin")
M.cos = M.func(math.cos, true, "cos")
M.tan = M.func(math.tan, true, "tan")
local msin = M.func(function(x) return -M.sin(x) end)
local mcos = M.func(function(x) return -M.cos(x) end)
local sec2inv = M.func(function(x) return 1/(M.cos(x))^2 end)
M.sin:setDerivative(M.cos)
M.cos:setDerivative(msin)
msin:setDerivative(mcos)
mcos:setDerivative(M.sin)
M.tan:setDerivative(sec2inv)
return M