-
Notifications
You must be signed in to change notification settings - Fork 11
/
formulas.py
701 lines (598 loc) · 12.9 KB
/
formulas.py
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
from __future__ import division
from xlsx import workbook, _flatten
import math, cmath, fractions, random
extend = workbook.extend #Decorator for adding new formulas
flatten = workbook.flatten #Formula doesn't care if a list is one or multi dimensional
singular = workbook.singular #Only takes in a single value
plural = workbook.plural #Takes multiple arguments, does not check for first param
void = workbook.void #Takes no arguments
def integrate(fn, lower, upper, threshold):
dx = float(upper-lower)/threshold
ddx = dx + lower
fn_dx = 0
while ddx < upper:
fn_dx += fn(ddx)*dx
ddx += dx
return fn_dx
def im(z):
return complex(str(z).replace("i", "j"))
def _round(x, sig, key=round):
sign = 1 if x>0 else -1
sig = -math.log10(abs(sig))
x = abs(x)
return sign*float(key(x*(10**sig)))/(10**sig)
def _roman(input):
if type(input) != type(1):
raise TypeError, "expected integer, got %s" % type(input)
if not 0 < input < 4000:
raise ValueError, "Argument must be between 1 and 3999"
ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
result = ""
for i in range(len(ints)):
count = int(input / ints[i])
result += nums[i] * count
input -= ints[i] * count
return result
@extend
@flatten
def SUM(list):
"Sums up the list of numbers"
sigma = 0
for n in list:
sigma += n
return sigma
@extend
@flatten
def AVERAGE(list):
"Find the average of the list of numbers"
sigma = 0.0
i = 0
for n in list:
sigma += n
i+=1
return sigma/i
@extend
@singular
def CODE(char):
"Return the first char code"
return ord(str(char)[0])
@extend
@plural
def BESSELI(base, nu):
"""
Returns the Modified BESSEL of First Kind function of parameters base and nu respectively.
BESSELI ~ (z,v): 1/pi integral_0^pie^(zcos(t)) cos(vt) dt
"""
dfn_dx = lambda x: (math.e**(base*math.cos(x)))*math.cos(nu*x)
return (1./math.pi)*integrate(dfn_dx, 0, math.pi, 5000)
@extend
@plural
def BESSELJ(base, nu):
"""
Returns the BESSEL Function of First Kind of parameters base and nu respectively.
BESSELJ ~ (z,v): 1/pi integral_0^pie^cos(zsin(t) - vt) dt
"""
dfn_dx = lambda x: math.cos(base*math.sin(x)-nu*x)
return (1./math.pi)*integrate(dfn_dx, 0, math.pi, 5000)
@extend
@plural
def BESSELK(base, nu):
"""
Returns the BESSEL Function of Second Kind of parameters base and nu respectively.
BESSELJ ~ (z,v): pi/2 (I-z(v)-Iz(v))/sin(z*pi)
BROKEN IMPLEMENTATION
"""
I = lambda n,x: (1./math.pi)*integrate(lambda x: (math.e**(base*math.cos(x)))*math.cos(nu*x), 0, math.pi, 5000)
#print 2*I(-1.5, 1)/(math.sin(base*math.pi))
K = (math.pi/2)*(I(-base, nu)-I(base, nu))/(math.sin(base*math.pi))
return K
@extend
@singular
def BIN2DEC(binary):
bin = list(str(binary))
dec = 0
for i in range(len(bin)):
dec += int(bin[::-1][i])*2**i
return dec
@extend
@singular
def DEC2BIN(dec):
return bin(dec).replace("0b","")
@extend
@singular
def BIN2HEX(binary):
bin = list(str(binary))
dec = 0
for i in range(len(bin)):
dec += int(bin[::-1][i])*2**i
return "%X" % dec
@extend
@singular
def DEC2HEX(dec):
return "%X"%dec
@extend
@singular
def BIN2OCT(binary):
bin = list(str(binary))
dec = 0
for i in range(len(bin)):
dec += int(bin[::-1][i])*2**i
return "%o" % dec
@extend
@singular
def DEC2OCT(dec):
return "%o"%dec
@extend
@singular
def HEX2DEC(dec):
return int(dec, 16)
@extend
@singular
def HEX2OCT(dec):
return "%o"%int(dec, 16)
@extend
@singular
def HEX2BIN(dec):
return bin(dec).replace("0b","")
@extend
@singular
def OCT2DEC(dec):
return int(dec, 8)
@extend
@singular
def OCT2HEX(dec):
return "%X"%int(dec, 8)
@extend
@singular
def OCT2BIN(dec):
return "%b"%int(dec, 8)
@extend
@plural
def COMPLEX(real, imaginary):
return complex(real, imaginary)
@extend
@plural
def DELTA(a, b):
return 1 if a == b else 0
@extend
@plural
def ERF(a, b = 0):
"""http://office.microsoft.com/en-us/excel/HP052090771033.aspx"""
def _erf(z):
return (2./math.sqrt(math.pi))*integrate(lambda x: math.e**(-x**2), 0, z, 5000)
if not b:
return _erf(a)
else:
return _erf(b) - _erf(a)
@extend
@singular
def ERFC(z):
def _erfc(z):
return (2./math.sqrt(math.pi))*integrate(lambda x: math.e**(-x**2), 0, z, 5000)
return 1 - _erfc(z)
@extend
@plural
def GESTEP(a,b):
return 1 if a >= b else 0
@extend
@singular
def IMABS(z):
c = im(z)
return abs(c)
@extend
@singular
def IMAGINARY(z):
c = im(z)
return c.imag
@extend
@singular
def IMARGUMENT(z):
c = im(z)
return cmath.phase(c)
@extend
@singular
def IMCONJUGATE(z):
c = im(z)
return c.conjugate()
@extend
@singular
def IMCOS(z):
c = im(z)
return cmath.cos(c)
@extend
@plural
def IMDIV(a,b):
a = im(a)
b = im(b)
return a/b
@extend
@singular
def IMEXP(z):
c = im(z)
return cmath.exp(c)
@extend
@singular
def IMLN(z):
c = im(z)
return cmath.log(c)
@extend
@singular
def IMLOG10(z):
c = im(z)
return cmath.log10(c)
@extend
@singular
def IMLOG2(z):
c = im(z)
return cmath.log(c, 2)
@extend
@plural
def IMPOWER(a,b):
a = im(a)
return a**b
@extend
@plural
def IMPRODUCT(a,b):
a = im(a)
b = im(b)
return a*b
@extend
@singular
def IMREAL(z):
c = im(z)
return c.real
@extend
@singular
def IMSIN(z):
c = im(z)
return cmath.sin(c)
@extend
@singular
def IMSQRT(z):
c = im(z)
return cmath.sqrt(c)
@extend
@plural
def IMSUB(a,b):
a = im(a)
b = im(b)
return a-b
@extend
@plural
def IMSUM(a,b):
a = im(a)
b = im(b)
return a+b
######################################
@extend
@singular
def ABS(x):
return abs(x)
@extend
@singular
def ACOS(x):
return math.acos(x)
@extend
@singular
def ACOSH(x):
return math.acosh(x)
@extend
@singular
def ASIN(x):
return math.asin(x)
@extend
@singular
def ASINH(x):
return math.asinh(x)
@extend
@singular
def ATAN(x):
return math.atan(x)
@extend
@singular
def ATANH(x):
return math.atanh(x)
@extend
@plural
def ATAN2(x, y):
return math.atan2(x, y)
@extend
@plural
def CEILING(x, sig):
sign = 1 if x>0 else -1
sig = -math.log10(abs(sig))
x = abs(x)
return float(math.ceil(x*(10**sig)))/(10**sig)
@extend
@plural
def COMBIN(n, k):
def _prod(fn, lower, upper, n = 1):
for i in range(lower, upper+1):n *= fn(i)
return n
x = lambda x: x if x else 1
return _prod(x, k+1, n)/_prod(x, 1, n-k)
@extend
@singular
def COS(x):
return math.cos(x)
@extend
@singular
def COSH(x):
return math.cosh(x)
@extend
@singular
def DEGREES(theta):
return theta/math.pi*180
@extend
@singular
def EVEN(x):
sign = 1 if x>0 else -1
if not _round(x, sign)%2: return int(_round(x, sign))
x = abs(x)
return sign*int(math.ceil(float(x)/2)*2)
@extend
@singular
def EXP(x):
return math.exp(x)
@extend
@singular
def FACT(n):
def _prod(fn, lower, upper, n = 1):
for i in range(int(lower), int(upper+1)):n *= fn(i)
return n
x = lambda x: x if x else 1
return _prod(x, 1, n)
@extend
@singular
def FACTDOUBLE(n):
x = 1
while n > 0:
x *= n
n -= 2
return x
@extend
@plural
def FLOOR(x, sig):
sign = 1 if x>0 else -1
sig = -math.log10(abs(sig))
x = abs(x)
return sign*float(math.floor(x*(10**sig)))/(10**sig)
@extend
@plural
def GCD(x, y):
return fractions.gcd(x, y)
@extend
@singular
def INT(x):
return int(math.floor(x))
@extend
@plural
def LCM(x, y):
return (x*y)/fractions.gcd(x, y)
@extend
@singular
def LN(x):
return math.log(x)
@extend
@plural
def LOG(x, y):
return math.log(x, y)
@extend
@singular
def LOG10(x):
return math.log10(x)
@extend
def MDETERM(matrix):
if len(matrix) is not len(matrix[0]): return
matrix = [[float(e) for e in m] for m in matrix]
def _det(matrix):
def _minor(matrix, i, j):
_matrix = [[e for e in m] for m in matrix]
_matrix.pop(i)
for _m in _matrix: _m.pop(j)
return _matrix
n = 0
if len(matrix) == 2:
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0] #ad-bc
for j in range(len(matrix)):
n += matrix[0][j]*(-1)**j*_det(_minor(matrix,0,j))
return n
return _det(matrix)
@extend
def MINVERSE(matrix):
if len(matrix) is not len(matrix[0]): return
matrix = [[float(e) for e in m] for m in matrix]
def _minor(matrix, i, j):
_matrix = [[e for e in m] for m in matrix]
_matrix.pop(i)
for _m in _matrix: _m.pop(j)
return _matrix
def _det(matrix):
n = 0
if len(matrix) == 2:
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0] #ad-bc
for j in range(len(matrix)):
n += matrix[0][j]*(-1)**j*_det(_minor(matrix,0,j))
return n
A_inv = 1./_det(matrix)
inverse = [[0 for e in m] for m in matrix]
if len(matrix) == 2:
matrix[0][0],matrix[1][1] = -matrix[1][1],-matrix[0][0]
return [[-A_inv*float(e) for e in m] for m in matrix]
for i in range(len(matrix)):
for j in range(len(matrix)):
inverse[j][i] = A_inv*(-1)**(i+j)*_det(_minor(matrix, i, j))
return inverse
@extend
@plural
def MMULT(matrix1, matrix2):
if len(matrix1[0]) is not len(matrix2[0]): return
_m = []
for row1 in matrix1:
_r = []
for col2 in zip(*matrix2):
_r += [sum([row1[i]*col2[i] for i in range(len(row1))])]
_m += [_r]
return _m
@extend
@plural
def MOD(a,b):
return a%b
@extend
@plural
def MROUND(a,b):
n = min(a%b, a%b-b, key = lambda a: abs(a)) if int(2*a%b - b) else a%b-b
return a-n
@extend
@plural
def MULTINOMIAL(*all):
def _fact(n):
return n*_fact(n-1) if n else 1
n = 1
for i in all:
n *= _fact(i)
return float(_fact(sum(all)))/n
@extend
@singular
def ODD(x):
sign = 1 if x>0 else -1
if _round(x, sign)%2: return int(_round(x, sign))
x = abs(x)
return sign*int(math.ceil(float(x)/2)*2 + 1)
@extend
@void
def PI(): return math.pi
@extend
@plural
def POWER(a,b):
return a**b
@extend
@flatten
def PRODUCT(list):
n = 1
for i in list:
n*=i
return n
@extend
@plural
def QUOTIENT(a,b):
return int(_round(a/b, 1, key=math.floor))
@extend
@singular
def RADIANS(theta):
return math.pi*theta/180
@extend
@void
def RAND():
return random.random()
@extend
@plural
def RANDBETWEEN(a,b):
return random.randint(a,b)
@extend
@plural
def ROMAN(a,b):
del b
return _roman(a)
@extend
@plural
def ROUND(n,sig):
return _round(n, 10**-sig)
@extend
@plural
def ROUNDDOWN(n,sig):
return _round(n, 10**-sig, key=math.floor)
@extend
@plural
def ROUNDUP(n,sig):
return _round(n, 10**-sig, key=math.ceil)
@extend
@plural
def SERIESSUM(x, n, m, a):
a = _flatten(a)
r = 0
for i in range(len(a)):
r += a[i]*(x**(n+m*i))
return r
@extend
@singular
def SIGN(x):
return 0 if not x else 1 if x>0 else -1
@extend
@singular
def SIN(x):
return math.sin(x)
@extend
@singular
def SINH(x):
return math.sinh(x)
@extend
@singular
def SQRT(x):
return math.sqrt(x)
@extend
@singular
def SQRTPI(x):
return math.sqrt(math.pi*x)
@extend
@flatten
def SUBTOTAL(all):
fn = all.pop(0)
fn_dict = {
1:workbook.AVERAGE,
2:workbook.COUNT,
3:workbook.COUNTA,
4:workbook.MAX,
5:workbook.MIN,
6:workbook.PRODUCT,
7:workbook.STDEV,
8:workbook.STDEVP,
9:workbook.SUM,
10:workbook.VAR,
11:workbook.VARP
}
return fn_dict[int(fn)](all)
@extend
@flatten
def SUM(list):
n = 0
for i in list:
n+=float(i)
return n
@extend
@plural
def SUMIF(a, cond, b = []):
a,b = _flatten(a),_flatten(b)
if not b: b = a
n= 0
o_cond = cond
if "<" not in str(cond) and ">" not in str(cond): cond = " == "+str(cond)
for i in range(len(a)):
try:
if eval(str(a[i]) + cond):
n+= float(b[i])
except:
if eval(str(a[i]) + "== '"+o_cond+"'"):
n+= float(b[i])
return n
@extend
def SUMPRODUCT(*list):
n = 0
for i in range(len(list)):
_n = 1
for l in list:
_n*=_flatten(l)[i]
n+=_n
return n
if __name__ == "__main__":
# I = lambda b,n: (1./cmath.pi)*integrate(lambda x: (cmath.e**(b*cmath.cos(x)))*cmath.cos(n*x), 0, cmath.pi, 5000)
#
# J = lambda b,n: (1./cmath.pi)*integrate(lambda x: cmath.cos(b*cmath.sin(x)-n*x), 0, cmath.pi, 5000)
#
# i = complex(0,1.)
# z = 2.5
# v = 1
# print math.sin(v*math.pi)
import demo
pass