symcalc 0.5 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/LICENSE.txt +202 -95
- data/lib/symcalc.rb +173 -31
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcfa9e29e383d1a24d3545c1944efac692a2e973861548dafeeaa61b34641147
|
4
|
+
data.tar.gz: 5bca902b27d64a837d3908b1bd835f519f1f8f707e4dc56fd99d5b971e9821c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49729eafc708ea1e59c50ad7f3c5d3a9dfd06ce21d673379e2050cc091b4ade9631d554e0d113db992f64303f5bd1f2c5ea0123a84aa36536a744d8d577d6e64
|
7
|
+
data.tar.gz: 7c84949657233ded9848633861fbd43963379b6fc468ef862cdb48830d2677547a1b7c8931d8e60d21356afef64cb5486229dcf29ff298af836a91b034321fcd
|
data/lib/LICENSE.txt
CHANGED
@@ -1,95 +1,202 @@
|
|
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
|
-
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/lib/symcalc.rb
CHANGED
@@ -1,11 +1,20 @@
|
|
1
|
+
# Copyright (c) 2024 Kyryl Shyshko
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
1
6
|
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
2
8
|
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License
|
6
|
-
# To view a copy of this license, visit https://creativecommons.org/licenses/by-sa/4.0/legalcode.en, or read LICENSE.txt
|
9
|
+
# or
|
7
10
|
#
|
11
|
+
# read LICENSE.txt in the SymCalc lib folder
|
8
12
|
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
9
18
|
|
10
19
|
|
11
20
|
class Equation
|
@@ -152,10 +161,8 @@ class Equation
|
|
152
161
|
|
153
162
|
end
|
154
163
|
|
155
|
-
# coeff = to_equation(coeff.eval({}))
|
156
164
|
coeff = coeff.__simplify__
|
157
165
|
|
158
|
-
|
159
166
|
if coeff == to_equation(1)
|
160
167
|
eq = eq
|
161
168
|
elsif coeff == to_equation(0)
|
@@ -185,28 +192,38 @@ class Equation
|
|
185
192
|
# puts fx.eval(x: 3)
|
186
193
|
#
|
187
194
|
def eval(var_hash)
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
var_hash.
|
196
|
-
|
197
|
-
computed << self.__eval__(hash)
|
198
|
-
end
|
199
|
-
return computed
|
195
|
+
if var_hash.values.size == 0
|
196
|
+
result = self.__eval__(Hash.new)
|
197
|
+
elsif !var_hash.values[0].is_a?(Array)
|
198
|
+
result = self.__eval__ var_hash
|
199
|
+
elsif var_hash.values[0].is_a? Array
|
200
|
+
result = []
|
201
|
+
var_hash.values[0].size.times do |i|
|
202
|
+
hash = var_hash.map {|k, v| [k, v[i]]}.to_h
|
203
|
+
result << self.__eval__(hash)
|
200
204
|
end
|
201
|
-
|
202
|
-
|
203
|
-
# return nil
|
204
|
-
# end
|
205
|
+
end
|
206
|
+
result
|
205
207
|
end
|
206
208
|
|
207
209
|
|
208
210
|
|
211
|
+
def __sub__ original, replacement
|
212
|
+
return to_equation(replacement) if self == to_equation(original)
|
213
|
+
return self
|
214
|
+
end
|
209
215
|
|
216
|
+
# Returns an equation with an expression substituted for the replacement
|
217
|
+
#
|
218
|
+
# Example:
|
219
|
+
# a = SymCalc.var("a")
|
220
|
+
# f = a ** 2
|
221
|
+
# x = SymCalc.var("x")
|
222
|
+
# puts f.sub(a, 3 * x) # => (3 * x) ** 2
|
223
|
+
#
|
224
|
+
def sub original, replacement
|
225
|
+
self.__sub__(original, replacement).simplify
|
226
|
+
end
|
210
227
|
end
|
211
228
|
|
212
229
|
# Converts the given argument into the equation type, if not already an equation
|
@@ -232,7 +249,11 @@ class EquationValue < Equation
|
|
232
249
|
end
|
233
250
|
|
234
251
|
def __eval__ var_hash
|
235
|
-
|
252
|
+
if @value.is_a? Integer
|
253
|
+
return @value.to_f
|
254
|
+
else
|
255
|
+
return @value
|
256
|
+
end
|
236
257
|
end
|
237
258
|
|
238
259
|
def __derivative__ variable: nil
|
@@ -240,11 +261,7 @@ class EquationValue < Equation
|
|
240
261
|
end
|
241
262
|
|
242
263
|
def ==(value)
|
243
|
-
|
244
|
-
return @value == value.value
|
245
|
-
else
|
246
|
-
return false
|
247
|
-
end
|
264
|
+
@value == value
|
248
265
|
end
|
249
266
|
|
250
267
|
def hash
|
@@ -293,7 +310,6 @@ class Variable < Equation
|
|
293
310
|
return @name
|
294
311
|
end
|
295
312
|
|
296
|
-
|
297
313
|
def __eval__ var_hash
|
298
314
|
if @fixed_value
|
299
315
|
return @fixed_value
|
@@ -380,6 +396,10 @@ class Sum < Equation
|
|
380
396
|
return (@lside.all_variables + @rside.all_variables).uniq
|
381
397
|
end
|
382
398
|
|
399
|
+
def __sub__ original, replacement
|
400
|
+
return to_equation(replacement) if self == to_equation(original)
|
401
|
+
return @lside.__sub__(original, replacement) + @rside.__sub__(original, replacement)
|
402
|
+
end
|
383
403
|
end
|
384
404
|
|
385
405
|
# Implements the subtraction operation in SymCalc
|
@@ -430,6 +450,11 @@ class Subtraction < Equation
|
|
430
450
|
def all_variables
|
431
451
|
return (@lside.all_variables + @rside.all_variables).uniq
|
432
452
|
end
|
453
|
+
|
454
|
+
def __sub__ original, replacement
|
455
|
+
return to_equation(replacement) if self == to_equation(original)
|
456
|
+
return @lside.__sub__(original, replacement) - @rside.__sub__(original, replacement)
|
457
|
+
end
|
433
458
|
end
|
434
459
|
|
435
460
|
# Implements the multiplication operation in SymCalc
|
@@ -482,6 +507,11 @@ class Multiplication < Equation
|
|
482
507
|
def all_variables
|
483
508
|
return (@lside.all_variables + @rside.all_variables).uniq
|
484
509
|
end
|
510
|
+
|
511
|
+
def __sub__ original, replacement
|
512
|
+
return to_equation(replacement) if self == to_equation(original)
|
513
|
+
return @lside.__sub__(original, replacement) * @rside.__sub__(original, replacement)
|
514
|
+
end
|
485
515
|
end
|
486
516
|
|
487
517
|
# Implements the division operation in SymCalc
|
@@ -542,6 +572,11 @@ class Division < Equation
|
|
542
572
|
def all_variables
|
543
573
|
return (@lside.all_variables + @rside.all_variables).uniq
|
544
574
|
end
|
575
|
+
|
576
|
+
def __sub__ original, replacement
|
577
|
+
return to_equation(replacement) if self == to_equation(original)
|
578
|
+
return @lside.__sub__(original, replacement) / @rside.__sub__(original, replacement)
|
579
|
+
end
|
545
580
|
end
|
546
581
|
|
547
582
|
|
@@ -596,6 +631,11 @@ class Exp < Equation
|
|
596
631
|
def all_variables
|
597
632
|
return @power.all_variables
|
598
633
|
end
|
634
|
+
|
635
|
+
def __sub__ original, replacement
|
636
|
+
return to_equation(replacement) if self == to_equation(original)
|
637
|
+
return Exp.new(@power.__sub__(original, replacement))
|
638
|
+
end
|
599
639
|
end
|
600
640
|
|
601
641
|
|
@@ -660,6 +700,10 @@ class Power < Equation
|
|
660
700
|
else
|
661
701
|
return base ** power
|
662
702
|
end
|
703
|
+
elsif base.is_a?(Power)
|
704
|
+
new_base = base.base
|
705
|
+
new_power = base.power * power
|
706
|
+
return (new_base ** new_power).simplify
|
663
707
|
else
|
664
708
|
return base ** power
|
665
709
|
end
|
@@ -669,6 +713,11 @@ class Power < Equation
|
|
669
713
|
def all_variables
|
670
714
|
return (@base.all_variables + @power.all_variables).uniq
|
671
715
|
end
|
716
|
+
|
717
|
+
def __sub__ original, replacement
|
718
|
+
return to_equation(replacement) if self == to_equation(original)
|
719
|
+
return @base.__sub__(original, replacement) ** @power.__sub__(original, replacement)
|
720
|
+
end
|
672
721
|
end
|
673
722
|
|
674
723
|
# Implements the sin operation in SymCalc
|
@@ -689,6 +738,10 @@ class Sin < Equation
|
|
689
738
|
return Cos.new(@eq) * @eq.derivative(variable: variable)
|
690
739
|
end
|
691
740
|
|
741
|
+
def __simplify__
|
742
|
+
return Sin.new(@eq.__simplify__)
|
743
|
+
end
|
744
|
+
|
692
745
|
def __get_m_elements__ var_hash
|
693
746
|
if var_hash.keys.include? self
|
694
747
|
var_hash[self] += 1
|
@@ -701,6 +754,11 @@ class Sin < Equation
|
|
701
754
|
def all_variables
|
702
755
|
return @eq.all_variables
|
703
756
|
end
|
757
|
+
|
758
|
+
def __sub__ original, replacement
|
759
|
+
return to_equation(replacement) if self == to_equation(original)
|
760
|
+
return Sin.new(@eq.__sub__(original, replacement))
|
761
|
+
end
|
704
762
|
end
|
705
763
|
|
706
764
|
# Implements the cos operation in SymCalc
|
@@ -721,6 +779,10 @@ class Cos < Equation
|
|
721
779
|
return -1 * Sin.new(@eq) * @eq.derivative(variable: variable)
|
722
780
|
end
|
723
781
|
|
782
|
+
def __simplify__
|
783
|
+
return Cos.new(@eq.__simplify__)
|
784
|
+
end
|
785
|
+
|
724
786
|
def __get_m_elements__ var_hash
|
725
787
|
if var_hash.keys.include? self
|
726
788
|
var_hash[self] += 1
|
@@ -733,6 +795,11 @@ class Cos < Equation
|
|
733
795
|
def all_variables
|
734
796
|
return @eq.all_variables
|
735
797
|
end
|
798
|
+
|
799
|
+
def __sub__ original, replacement
|
800
|
+
return to_equation(replacement) if self == to_equation(original)
|
801
|
+
return Cos.new(@eq.__sub__(original, replacement))
|
802
|
+
end
|
736
803
|
end
|
737
804
|
|
738
805
|
|
@@ -757,6 +824,10 @@ class Ln < Equation
|
|
757
824
|
return 1 / @eq * @eq.derivative(variable: variable)
|
758
825
|
end
|
759
826
|
|
827
|
+
def __simplify__
|
828
|
+
return Ln.new(@eq.__simplify__)
|
829
|
+
end
|
830
|
+
|
760
831
|
def __get_m_elements__ var_hash
|
761
832
|
if var_hash.keys.include? self
|
762
833
|
var_hash[self] += 1
|
@@ -769,6 +840,11 @@ class Ln < Equation
|
|
769
840
|
def all_variables
|
770
841
|
return @eq.all_variables
|
771
842
|
end
|
843
|
+
|
844
|
+
def __sub__ original, replacement
|
845
|
+
return to_equation(replacement) if self == to_equation(original)
|
846
|
+
return Ln.new(@eq.__sub__(original, replacement))
|
847
|
+
end
|
772
848
|
end
|
773
849
|
|
774
850
|
# Implements the logarithm operation in SymCalc
|
@@ -776,7 +852,7 @@ class Log < Equation
|
|
776
852
|
|
777
853
|
attr_accessor :eq, :base
|
778
854
|
|
779
|
-
# Implements the
|
855
|
+
# Implements the logarithm operation in SymCalc
|
780
856
|
# Accepts the base and equation arguments
|
781
857
|
#
|
782
858
|
# Example:
|
@@ -801,6 +877,10 @@ class Log < Equation
|
|
801
877
|
return 1 / (Ln.new(@base) * @eq) * @eq.derivative(variable: variable)
|
802
878
|
end
|
803
879
|
|
880
|
+
def __simplify__
|
881
|
+
return Log.new(@base.__simplify__, @eq.__simplify__)
|
882
|
+
end
|
883
|
+
|
804
884
|
def __get_m_elements__ var_hash
|
805
885
|
if var_hash.keys.include? self
|
806
886
|
var_hash[self] += 1
|
@@ -813,8 +893,65 @@ class Log < Equation
|
|
813
893
|
def all_variables
|
814
894
|
return (@base.all_variables + @eq.all_variables).uniq
|
815
895
|
end
|
896
|
+
|
897
|
+
def __sub__ original, replacement
|
898
|
+
return to_equation(replacement) if self == to_equation(original)
|
899
|
+
return Log.new(@base.__sub__(original, replacement), @eq.__sub__(original, replacement))
|
900
|
+
end
|
816
901
|
end
|
817
902
|
|
903
|
+
# Implements the absolute value operation in SymCalc
|
904
|
+
class Abs < Equation
|
905
|
+
|
906
|
+
attr_accessor :eq
|
907
|
+
|
908
|
+
def initialize eq
|
909
|
+
@eq = to_equation(eq)
|
910
|
+
end
|
911
|
+
|
912
|
+
def display
|
913
|
+
return "|#{@eq.display}|"
|
914
|
+
end
|
915
|
+
|
916
|
+
def __eval__ var_hash
|
917
|
+
return @eq.eval(var_hash).abs
|
918
|
+
end
|
919
|
+
|
920
|
+
def __derivative__ variable: nil
|
921
|
+
return @eq / Abs.new(@eq) * @eq.derivative(variable: variable)
|
922
|
+
end
|
923
|
+
|
924
|
+
def __simplify__
|
925
|
+
return Abs.new(@eq.__simplify__)
|
926
|
+
end
|
927
|
+
|
928
|
+
def __get_m_elements__ var_hash
|
929
|
+
if var_hash.keys.include? self
|
930
|
+
var_hash[self] += 1
|
931
|
+
else
|
932
|
+
var_hash[self] = 1
|
933
|
+
end
|
934
|
+
return var_hash
|
935
|
+
end
|
936
|
+
|
937
|
+
def __simplify__
|
938
|
+
if @eq.is_a?(Power) && @eq.power.is_a?(EquationValue) && @eq.power.value.is_a?(Numeric) && (@eq.power.value % 2 == 0)
|
939
|
+
return @eq
|
940
|
+
end
|
941
|
+
self
|
942
|
+
end
|
943
|
+
|
944
|
+
def all_variables
|
945
|
+
return @eq.all_variables
|
946
|
+
end
|
947
|
+
|
948
|
+
def __sub__ original, replacement
|
949
|
+
return to_equation(replacement) if self == to_equation(original)
|
950
|
+
return Abs.new(@eq.__sub__(original, replacement))
|
951
|
+
end
|
952
|
+
end
|
953
|
+
|
954
|
+
|
818
955
|
# The SymCalc module implemenets the standard function class creations shorter
|
819
956
|
module SymCalc
|
820
957
|
|
@@ -848,6 +985,11 @@ module SymCalc
|
|
848
985
|
Variable.new name, fixed_value
|
849
986
|
end
|
850
987
|
|
851
|
-
|
988
|
+
# abs(equation) is the same as Abs.new(equation), just shorter
|
989
|
+
def abs eq
|
990
|
+
return Abs.new(to_equation(eq))
|
991
|
+
end
|
992
|
+
|
993
|
+
module_function :sin, :cos, :ln, :log, :exp, :var, :abs
|
852
994
|
|
853
995
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: symcalc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyryl Shyshko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: SymCalc empowers developers with the ability of creating symbolic functions,
|
14
|
-
|
14
|
+
evaluating them and finding their derivatives in code.
|
15
15
|
email: kyryloshy@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
@@ -21,7 +21,7 @@ files:
|
|
21
21
|
- lib/symcalc.rb
|
22
22
|
homepage: https://rubygems.org/gems/symcalc
|
23
23
|
licenses:
|
24
|
-
-
|
24
|
+
- Apache-2.0
|
25
25
|
metadata: {}
|
26
26
|
post_install_message:
|
27
27
|
rdoc_options: []
|
@@ -41,6 +41,5 @@ requirements: []
|
|
41
41
|
rubygems_version: 3.3.11
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
|
-
summary:
|
45
|
-
in code.
|
44
|
+
summary: Mathematical symbolic functions, calculus and differentiation in code
|
46
45
|
test_files: []
|