-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathrationalize.coffee
More file actions
156 lines (110 loc) · 2.37 KB
/
rationalize.coffee
File metadata and controls
156 lines (110 loc) · 2.37 KB
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
Eval_rationalize = ->
push(cadr(p1))
Eval()
rationalize()
rationalize = ->
x = expanding
yyrationalize()
expanding = x
yyrationalize = ->
theArgument = pop()
if (istensor(theArgument))
__rationalize_tensor(theArgument)
return
expanding = 0
if (car(theArgument) != symbol(ADD))
push(theArgument)
return
if DEBUG
printf("rationalize: this is the input expr:\n")
printline(theArgument)
# get common denominator
push(one)
multiply_denominators(theArgument)
commonDenominator = pop()
if DEBUG
printf("rationalize: this is the common denominator:\n")
printline(commonDenominator)
# multiply each term by common denominator
push(zero)
eachTerm = cdr(theArgument)
while (iscons(eachTerm))
push(commonDenominator)
push(car(eachTerm))
multiply()
add()
eachTerm = cdr(eachTerm)
if DEBUG
printf("rationalize: original expr times common denominator:\n")
printline(stack[tos - 1])
# collect common factors
Condense()
if DEBUG
printf("rationalize: after factoring:\n")
printline(stack[tos - 1])
# divide by common denominator
push(commonDenominator)
divide()
if DEBUG
printf("rationalize: after dividing by common denom. (and we're done):\n")
printline(stack[tos - 1])
multiply_denominators = (p) ->
if (car(p) == symbol(ADD))
p = cdr(p)
while (iscons(p))
multiply_denominators_term(car(p))
p = cdr(p)
else
multiply_denominators_term(p)
multiply_denominators_term = (p) ->
if (car(p) == symbol(MULTIPLY))
p = cdr(p)
while (iscons(p))
multiply_denominators_factor(car(p))
p = cdr(p)
else
multiply_denominators_factor(p)
multiply_denominators_factor = (p) ->
if (car(p) != symbol(POWER))
return
push(p)
p = caddr(p)
# like x^(-2) ?
if (isnegativenumber(p))
inverse()
__lcm()
return
# like x^(-a) ?
if (car(p) == symbol(MULTIPLY) && isnegativenumber(cadr(p)))
inverse()
__lcm()
return
# no match
pop()
__rationalize_tensor = (theTensor) ->
i = 0
push(theTensor)
Eval(); # makes a copy
theTensor = pop()
if (!istensor(theTensor)) # might be zero
push(theTensor)
return
n = theTensor.tensor.nelem
for i in [0...n]
push(theTensor.tensor.elem[i])
rationalize()
theTensor.tensor.elem[i] = pop()
check_tensor_dimensions theTensor
push(theTensor)
__lcm = ->
save()
p1 = pop()
p2 = pop()
push(p1)
push(p2)
multiply()
push(p1)
push(p2)
gcd()
divide()
restore()