mathmas 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2debb3fd1629372497b194b8d3d5c9205556e3f2
4
+ data.tar.gz: cd33b3d9e279e28e9b1673752f87837c4b190249
5
+ SHA512:
6
+ metadata.gz: ba4af8c5a9a14c6e8e80f0a474e357502b2605bb9a42cb7a7c90ae7e6f5b959ee8832b5aa87c415ffc670183f276d586c46c7d972673f8a673817331904872ba
7
+ data.tar.gz: 273f75d51e0767d322bbb02e930f6686506ce857f6f02d06a425a7635048547102d858697863618d88131e819fa7ed9aff4fe57cdb40ff1e06021000bf2815d8
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mathmas.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Naoki Nishida
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # Mathmas
2
+
3
+ Mathmas is a gem for Symbolic Mathematics inspired by [SymPy](https://github.com/sympy/sympy) and [dydx](https://github.com/gogotanaka/dydx). This gem is still an experimental implementation and needs your help. Please contact me if you are interest in contributing it.
4
+
5
+ The word "Mathmas" comes from "ますます"(Mas-Mas) which means "more and more" in Japanese.
6
+
7
+ **This gem is still on progress. See [the notebook](http://nbviewer.ipython.org/github/domitry/mathmas/blob/master/examples/Mathmas_develop.ipynb) to overview the current status.**
8
+
9
+ ## Quick Start
10
+
11
+ Run the code below to install Mathmas.
12
+
13
+ ```shell
14
+ gem install mathmas
15
+ ```
16
+
17
+ Then try example.
18
+
19
+ ```shell
20
+ cp path_to_gem/examples/Mathmas_develop.ipynb ~
21
+ cd ~
22
+ iruby notebook #-> then select "Mathmas_develop" on your browser.
23
+ ```
24
+
25
+ ## Feature
26
+
27
+ + Simple Symbolic Equation
28
+ + Beautiful Displaying of numerical formula on [IRuby notebook](https://github.com/minad/iruby)
29
+ + Plotting with [Nyaplot](https://github.com/domitry/nyaplot)
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ require 'mathmas'
35
+ include Mathmas
36
+ f(x) = x**2
37
+ f.plot(x: -1..1) #-> Plot on IRuby notebook
38
+ f.diff #-> 2*x # not implemented yet
39
+ f.integrate(x: -1..1) # not implemented yet
40
+ f.minimize # not implemented yet
41
+ ```
42
+
43
+ ## TODO:
44
+ + Differentiating
45
+ + Definite Integrating
46
+ + Undefinite Integrating
47
+ + Optimization problem solver
48
+ + ODE solver
49
+
50
+ ## Installation
51
+
52
+ Add this line to your application's Gemfile:
53
+
54
+ gem 'mathmas'
55
+
56
+ And then execute:
57
+
58
+ $ bundle
59
+
60
+ Or install it yourself as:
61
+
62
+ $ gem install mathmas
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( http://github.com/<my-github-username>/mathmas/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,293 @@
1
+ {
2
+ "metadata": {
3
+ "language": "ruby",
4
+ "name": ""
5
+ },
6
+ "nbformat": 3,
7
+ "nbformat_minor": 0,
8
+ "worksheets": [
9
+ {
10
+ "cells": [
11
+ {
12
+ "cell_type": "code",
13
+ "collapsed": false,
14
+ "input": [
15
+ "require 'mathmas'"
16
+ ],
17
+ "language": "python",
18
+ "metadata": {},
19
+ "outputs": [
20
+ {
21
+ "html": [
22
+ "<script type='text/javascript'>if(window['d3'] === undefined ||\n",
23
+ " window['Nyaplot'] === undefined){\n",
24
+ " var path = {\"d3\":\"http://d3js.org/d3.v3.min\"};\n",
25
+ "\n",
26
+ "\n",
27
+ "\n",
28
+ " var shim = {\"d3\":{\"exports\":\"d3\"}};\n",
29
+ "\n",
30
+ " require.config({paths: path, shim:shim});\n",
31
+ "\n",
32
+ "\n",
33
+ "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');\n",
34
+ "\n",
35
+ "\tvar script = d3.select(\"head\")\n",
36
+ "\t .append(\"script\")\n",
37
+ "\t .attr(\"src\", \"https://rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n",
38
+ "\t .attr(\"async\", true);\n",
39
+ "\n",
40
+ "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n",
41
+ "\n",
42
+ "\n",
43
+ "\t var event = document.createEvent(\"HTMLEvents\");\n",
44
+ "\t event.initEvent(\"load_nyaplot\",false,false);\n",
45
+ "\t window.dispatchEvent(event);\n",
46
+ "\t console.log('Finished loading Nyaplotjs');\n",
47
+ "\n",
48
+ "\t};\n",
49
+ "\n",
50
+ "\n",
51
+ "});\n",
52
+ "}\n",
53
+ "</script>"
54
+ ],
55
+ "metadata": {},
56
+ "output_type": "pyout",
57
+ "prompt_number": 1,
58
+ "text": [
59
+ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"http://d3js.org/d3.v3.min\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});\\n}\\n\""
60
+ ]
61
+ },
62
+ {
63
+ "metadata": {},
64
+ "output_type": "pyout",
65
+ "prompt_number": 1,
66
+ "text": [
67
+ "true"
68
+ ]
69
+ }
70
+ ],
71
+ "prompt_number": 1
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "collapsed": false,
76
+ "input": [
77
+ "include Mathmas"
78
+ ],
79
+ "language": "python",
80
+ "metadata": {},
81
+ "outputs": [
82
+ {
83
+ "metadata": {},
84
+ "output_type": "pyout",
85
+ "prompt_number": 2,
86
+ "text": [
87
+ "Object"
88
+ ]
89
+ }
90
+ ],
91
+ "prompt_number": 2
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "collapsed": false,
96
+ "input": [
97
+ "expr = 1/x"
98
+ ],
99
+ "language": "python",
100
+ "metadata": {},
101
+ "outputs": [
102
+ {
103
+ "html": [
104
+ "<script type=\"math/tex; mode=display\">\\frac{1}{x}</script>"
105
+ ],
106
+ "metadata": {},
107
+ "output_type": "pyout",
108
+ "prompt_number": 3,
109
+ "text": [
110
+ "#<Mathmas::Power:0xb926e404 @args=[#<Mathmas::Variable:0xb926e454 @symbol=:x>, #<Mathmas::Number:0xb926e418 @num=-1>]>"
111
+ ]
112
+ }
113
+ ],
114
+ "prompt_number": 3
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "collapsed": false,
119
+ "input": [
120
+ "expr.exec(x: 3)"
121
+ ],
122
+ "language": "python",
123
+ "metadata": {},
124
+ "outputs": [
125
+ {
126
+ "metadata": {},
127
+ "output_type": "pyout",
128
+ "prompt_number": 4,
129
+ "text": [
130
+ "(1/3)"
131
+ ]
132
+ }
133
+ ],
134
+ "prompt_number": 4
135
+ },
136
+ {
137
+ "cell_type": "code",
138
+ "collapsed": false,
139
+ "input": [
140
+ "expr.exec(x: 1)"
141
+ ],
142
+ "language": "python",
143
+ "metadata": {},
144
+ "outputs": [
145
+ {
146
+ "metadata": {},
147
+ "output_type": "pyout",
148
+ "prompt_number": 14,
149
+ "text": [
150
+ "1"
151
+ ]
152
+ }
153
+ ],
154
+ "prompt_number": 14
155
+ },
156
+ {
157
+ "cell_type": "code",
158
+ "collapsed": false,
159
+ "input": [
160
+ "f(x) <= 1/x"
161
+ ],
162
+ "language": "python",
163
+ "metadata": {},
164
+ "outputs": [
165
+ {
166
+ "html": [
167
+ "<script type=\"math/tex; mode=display\">\\frac{1}{x}</script>"
168
+ ],
169
+ "metadata": {},
170
+ "output_type": "pyout",
171
+ "prompt_number": 5,
172
+ "text": [
173
+ "#<Mathmas::Power:0xb929d36c @args=[#<Mathmas::Variable:0xb929d3bc @symbol=:x>, #<Mathmas::Number:0xb929d380 @num=-1>]>"
174
+ ]
175
+ }
176
+ ],
177
+ "prompt_number": 5
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "collapsed": false,
182
+ "input": [
183
+ "f"
184
+ ],
185
+ "language": "python",
186
+ "metadata": {},
187
+ "outputs": [
188
+ {
189
+ "html": [
190
+ "<script type=\"math/tex; mode=display\">f(x) = \\frac{1}{x}</script>"
191
+ ],
192
+ "metadata": {},
193
+ "output_type": "pyout",
194
+ "prompt_number": 6,
195
+ "text": [
196
+ "#<Mathmas::Function:0xb929d470 @expr=#<Mathmas::Power:0xb929d36c @args=[#<Mathmas::Variable:0xb929d3bc @symbol=:x>, #<Mathmas::Number:0xb929d380 @num=-1>]>, @name=:f, @vals=[#<Mathmas::Variable:0xb929d538 @symbol=:x>]>"
197
+ ]
198
+ }
199
+ ],
200
+ "prompt_number": 6
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "collapsed": false,
205
+ "input": [
206
+ "f(3)"
207
+ ],
208
+ "language": "python",
209
+ "metadata": {},
210
+ "outputs": [
211
+ {
212
+ "metadata": {},
213
+ "output_type": "pyout",
214
+ "prompt_number": 7,
215
+ "text": [
216
+ "(1/3)"
217
+ ]
218
+ }
219
+ ],
220
+ "prompt_number": 7
221
+ },
222
+ {
223
+ "cell_type": "code",
224
+ "collapsed": false,
225
+ "input": [
226
+ "f.plot(x: 1..3)"
227
+ ],
228
+ "language": "python",
229
+ "metadata": {},
230
+ "outputs": [
231
+ {
232
+ "output_type": "stream",
233
+ "stream": "stdout",
234
+ "text": [
235
+ "[\"x\"]"
236
+ ]
237
+ },
238
+ {
239
+ "html": [
240
+ "<div id='vis-3d47f722-9a77-44d2-8740-c565324b29c7'></div>\n",
241
+ "<script>\n",
242
+ "(function(){\n",
243
+ " var render = function(){\n",
244
+ " var model = {\"panes\":[{\"diagrams\":[{\"type\":\"line\",\"options\":{\"x\":\"data0\",\"y\":\"data1\"},\"data\":\"8e5690f7-0b93-4997-8ccb-5a25e77fa843\"}],\"options\":{\"x_label\":\"x\",\"y_label\":\"f(x) = x^-1\",\"zoom\":true,\"width\":700,\"xrange\":[1.0,3.0],\"yrange\":[0.3333333333333333,1.0]}}],\"data\":{\"8e5690f7-0b93-4997-8ccb-5a25e77fa843\":[{\"data0\":1.0,\"data1\":1.0},{\"data0\":1.02020202020202,\"data1\":0.9801980198019803},{\"data0\":1.0404040404040404,\"data1\":0.9611650485436893},{\"data0\":1.0606060606060606,\"data1\":0.942857142857143},{\"data0\":1.0808080808080809,\"data1\":0.925233644859813},{\"data0\":1.101010101010101,\"data1\":0.908256880733945},{\"data0\":1.121212121212121,\"data1\":0.891891891891892},{\"data0\":1.1414141414141414,\"data1\":0.8761061946902655},{\"data0\":1.1616161616161615,\"data1\":0.8608695652173913},{\"data0\":1.1818181818181819,\"data1\":0.8461538461538461},{\"data0\":1.202020202020202,\"data1\":0.8319327731092437},{\"data0\":1.2222222222222223,\"data1\":0.8181818181818181},{\"data0\":1.2424242424242424,\"data1\":0.8048780487804879},{\"data0\":1.2626262626262625,\"data1\":0.792},{\"data0\":1.2828282828282829,\"data1\":0.7795275590551181},{\"data0\":1.303030303030303,\"data1\":0.7674418604651163},{\"data0\":1.3232323232323233,\"data1\":0.7557251908396946},{\"data0\":1.3434343434343434,\"data1\":0.7443609022556391},{\"data0\":1.3636363636363638,\"data1\":0.7333333333333333},{\"data0\":1.3838383838383839,\"data1\":0.7226277372262774},{\"data0\":1.404040404040404,\"data1\":0.7122302158273381},{\"data0\":1.4242424242424243,\"data1\":0.7021276595744681},{\"data0\":1.4444444444444444,\"data1\":0.6923076923076923},{\"data0\":1.4646464646464648,\"data1\":0.6827586206896551},{\"data0\":1.4848484848484849,\"data1\":0.673469387755102},{\"data0\":1.5050505050505052,\"data1\":0.6644295302013422},{\"data0\":1.5252525252525253,\"data1\":0.6556291390728477},{\"data0\":1.5454545454545454,\"data1\":0.6470588235294118},{\"data0\":1.5656565656565657,\"data1\":0.6387096774193548},{\"data0\":1.5858585858585859,\"data1\":0.6305732484076433},{\"data0\":1.606060606060606,\"data1\":0.6226415094339623},{\"data0\":1.6262626262626263,\"data1\":0.6149068322981366},{\"data0\":1.6464646464646466,\"data1\":0.607361963190184},{\"data0\":1.6666666666666667,\"data1\":0.6},{\"data0\":1.6868686868686869,\"data1\":0.592814371257485},{\"data0\":1.7070707070707072,\"data1\":0.5857988165680473},{\"data0\":1.7272727272727273,\"data1\":0.5789473684210527},{\"data0\":1.7474747474747474,\"data1\":0.5722543352601156},{\"data0\":1.7676767676767677,\"data1\":0.5657142857142857},{\"data0\":1.787878787878788,\"data1\":0.559322033898305},{\"data0\":1.8080808080808082,\"data1\":0.5530726256983239},{\"data0\":1.8282828282828283,\"data1\":0.5469613259668509},{\"data0\":1.8484848484848486,\"data1\":0.540983606557377},{\"data0\":1.8686868686868687,\"data1\":0.5351351351351351},{\"data0\":1.8888888888888888,\"data1\":0.5294117647058824},{\"data0\":1.9090909090909092,\"data1\":0.5238095238095238},{\"data0\":1.9292929292929295,\"data1\":0.5183246073298429},{\"data0\":1.9494949494949496,\"data1\":0.5129533678756476},{\"data0\":1.9696969696969697,\"data1\":0.5076923076923077},{\"data0\":1.98989898989899,\"data1\":0.5025380710659898},{\"data0\":2.0101010101010104,\"data1\":0.4974874371859296},{\"data0\":2.0303030303030303,\"data1\":0.49253731343283585},{\"data0\":2.0505050505050506,\"data1\":0.48768472906403937},{\"data0\":2.070707070707071,\"data1\":0.4829268292682926},{\"data0\":2.090909090909091,\"data1\":0.4782608695652174},{\"data0\":2.111111111111111,\"data1\":0.47368421052631576},{\"data0\":2.1313131313131315,\"data1\":0.4691943127962085},{\"data0\":2.1515151515151514,\"data1\":0.46478873239436624},{\"data0\":2.1717171717171717,\"data1\":0.4604651162790698},{\"data0\":2.191919191919192,\"data1\":0.456221198156682},{\"data0\":2.212121212121212,\"data1\":0.452054794520548},{\"data0\":2.2323232323232327,\"data1\":0.4479638009049773},{\"data0\":2.2525252525252526,\"data1\":0.4439461883408072},{\"data0\":2.272727272727273,\"data1\":0.43999999999999995},{\"data0\":2.2929292929292933,\"data1\":0.4361233480176211},{\"data0\":2.313131313131313,\"data1\":0.43231441048034935},{\"data0\":2.3333333333333335,\"data1\":0.42857142857142855},{\"data0\":2.353535353535354,\"data1\":0.42489270386266087},{\"data0\":2.3737373737373737,\"data1\":0.42127659574468085},{\"data0\":2.393939393939394,\"data1\":0.4177215189873418},{\"data0\":2.4141414141414144,\"data1\":0.4142259414225941},{\"data0\":2.4343434343434343,\"data1\":0.41078838174273863},{\"data0\":2.4545454545454546,\"data1\":0.4074074074074074},{\"data0\":2.474747474747475,\"data1\":0.4040816326530612},{\"data0\":2.494949494949495,\"data1\":0.4008097165991903},{\"data0\":2.5151515151515156,\"data1\":0.3975903614457831},{\"data0\":2.5353535353535355,\"data1\":0.3944223107569721},{\"data0\":2.5555555555555554,\"data1\":0.391304347826087},{\"data0\":2.575757575757576,\"data1\":0.388235294117647},{\"data0\":2.595959595959596,\"data1\":0.38521400778210113},{\"data0\":2.6161616161616164,\"data1\":0.3822393822393822},{\"data0\":2.6363636363636367,\"data1\":0.3793103448275862},{\"data0\":2.6565656565656566,\"data1\":0.376425855513308},{\"data0\":2.676767676767677,\"data1\":0.37358490566037733},{\"data0\":2.6969696969696972,\"data1\":0.37078651685393255},{\"data0\":2.717171717171717,\"data1\":0.3680297397769517},{\"data0\":2.7373737373737375,\"data1\":0.36531365313653136},{\"data0\":2.757575757575758,\"data1\":0.3626373626373626},{\"data0\":2.7777777777777777,\"data1\":0.36},{\"data0\":2.7979797979797985,\"data1\":0.35740072202166057},{\"data0\":2.8181818181818183,\"data1\":0.3548387096774193},{\"data0\":2.8383838383838382,\"data1\":0.3523131672597865},{\"data0\":2.858585858585859,\"data1\":0.34982332155477025},{\"data0\":2.878787878787879,\"data1\":0.34736842105263155},{\"data0\":2.8989898989898992,\"data1\":0.34494773519163763},{\"data0\":2.9191919191919196,\"data1\":0.34256055363321797},{\"data0\":2.9393939393939394,\"data1\":0.3402061855670103},{\"data0\":2.95959595959596,\"data1\":0.3378839590443686},{\"data0\":2.97979797979798,\"data1\":0.33559322033898303},{\"data0\":3.0,\"data1\":0.3333333333333333}]},\"extension\":[]}\n",
245
+ " Nyaplot.core.parse(model, '#vis-3d47f722-9a77-44d2-8740-c565324b29c7');\n",
246
+ " };\n",
247
+ " if(window['Nyaplot']==undefined){\n",
248
+ " window.addEventListener('load_nyaplot', render, false);\n",
249
+ "\treturn;\n",
250
+ " } else {\n",
251
+ " render();\n",
252
+ " }\n",
253
+ "})();\n",
254
+ "</script>\n"
255
+ ],
256
+ "metadata": {},
257
+ "output_type": "pyout",
258
+ "prompt_number": 9,
259
+ "text": [
260
+ "#<Nyaplot::Plot:0xb884c82c @properties={:diagrams=>[#<Nyaplot::Diagram:0xb8bbb454 @properties={:type=>:line, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"8e5690f7-0b93-4997-8ccb-5a25e77fa843\"}, @xrange=[1.0, 3.0], @yrange=[0.3333333333333333, 1.0]>], :options=>{:x_label=>:x, :y_label=>\"f(x) = x^-1\", :zoom=>true, :width=>700, :xrange=>[1.0, 3.0], :yrange=>[0.3333333333333333, 1.0]}}>"
261
+ ]
262
+ }
263
+ ],
264
+ "prompt_number": 9
265
+ },
266
+ {
267
+ "cell_type": "code",
268
+ "collapsed": false,
269
+ "input": [
270
+ "g(x, y) <= (x + y)/(x**3 + y**2)"
271
+ ],
272
+ "language": "python",
273
+ "metadata": {},
274
+ "outputs": [
275
+ {
276
+ "html": [
277
+ "<script type=\"math/tex; mode=display\">(x + y)*(\\frac{1}{(x^3 + y^2)})</script>"
278
+ ],
279
+ "metadata": {},
280
+ "output_type": "pyout",
281
+ "prompt_number": 11,
282
+ "text": [
283
+ "#<Mathmas::Multiply:0xb8c586c8 @args=[#<Mathmas::Plus:0xb8c58970 @args=[#<Mathmas::Variable:0xb8c58a24 @symbol=:x>, #<Mathmas::Variable:0xb8c58984 @symbol=:y>]>, #<Mathmas::Power:0xb8c586f0 @args=[#<Mathmas::Plus:0xb8c58740 @args=[#<Mathmas::Power:0xb8c58858 @args=[#<Mathmas::Variable:0xb8c58894 @symbol=:x>, #<Mathmas::Number:0xb8c5886c @num=3>]>, #<Mathmas::Power:0xb8c58768 @args=[#<Mathmas::Variable:0xb8c587a4 @symbol=:y>, #<Mathmas::Number:0xb8c58790 @num=2>]>]>, #<Mathmas::Number:0xb8c58704 @num=-1>]>]>"
284
+ ]
285
+ }
286
+ ],
287
+ "prompt_number": 11
288
+ }
289
+ ],
290
+ "metadata": {}
291
+ }
292
+ ]
293
+ }
@@ -0,0 +1,8 @@
1
+ require_relative "mathmas/monkey"
2
+ require_relative "mathmas/version"
3
+ require_relative "mathmas/core/basic"
4
+ require_relative "mathmas/core/number"
5
+ require_relative "mathmas/core/symbol"
6
+ require_relative "mathmas/core/expression"
7
+ require_relative "mathmas/core/function"
8
+ require_relative "mathmas/plot/plot"
File without changes
File without changes
File without changes
@@ -0,0 +1,48 @@
1
+ module Mathmas
2
+ module Basic
3
+ # TODO: delete the line arg=( ? : )
4
+ def +(arg)
5
+ arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
6
+ Plus.new(self, arg)
7
+ end
8
+
9
+ def -(arg)
10
+ arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
11
+ Plus.new(self, Multiply.new(Number.new(-1), arg))
12
+ end
13
+
14
+ def *(arg)
15
+ arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
16
+ Multiply.new(self, arg)
17
+ end
18
+
19
+ def /(arg)
20
+ arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
21
+ if self.is_a?(Number) && self.num == 1
22
+ Power.new(arg, Number.new(-1))
23
+ else
24
+ Multiply.new(self, Power.new(arg, Number.new(-1)))
25
+ end
26
+ end
27
+
28
+ def **(arg)
29
+ arg = (arg.is_a?(Numeric) ? Number.new(arg) : arg)
30
+ Power.new(self, arg)
31
+ end
32
+
33
+ def coerce(other)
34
+ if other.is_a? Numeric
35
+ return Mathmas::Number.new(other), self
36
+ else
37
+ raise "There is no rule for handling #{other.to_s}."
38
+ end
39
+ end
40
+
41
+ def to_iruby
42
+ # Dirty Hack for IRuby. I hope IRuby will fix the problem in the future.
43
+ # ["text/latex", self.to_tex] not work.
44
+ html = "<script type=\"math/tex; mode=display\">#{ self.to_tex }</script>"
45
+ ['text/html', html]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,106 @@
1
+ module Mathmas
2
+ # Currently Mathmas have 3 types of expression, Plus, Multiply and Power.
3
+ # All calcs including dividing are expressed with these three.
4
+ #
5
+ # @example
6
+ # 1/(x + y)
7
+ #
8
+ # Multiply(Number(1), Power(Plus(Symbol(x), Symbol(y), -1))
9
+ #
10
+ class Expression
11
+ include Mathmas::Basic
12
+
13
+ def initialize(*args)
14
+ @args = args
15
+ end
16
+
17
+ # @example
18
+ # (1/x).exec(x: 3) #-> 1/3
19
+ # (1/(x*y)).exec(x: 3) #-> 1/(3*x)
20
+ def exec(args)
21
+ arr = @args.map do |arg|
22
+ if arg.is_a?(Variable)
23
+ unless args.keys.index(arg.symbol).nil?
24
+ next args[arg.symbol]
25
+ else
26
+ next arg
27
+ end
28
+ elsif arg.is_a?(Expression)
29
+ next arg.exec(args)
30
+ elsif arg.is_a?(Number)
31
+ next arg.num
32
+ else
33
+ raise "Invailed Arguments"
34
+ end
35
+ end
36
+ arr
37
+ end
38
+ end
39
+
40
+ class Plus < Expression
41
+ def to_s
42
+ @args.map{|arg| arg.to_s}.join(" + ")
43
+ end
44
+
45
+ def to_tex
46
+ @args.map{|arg| arg.to_tex}.join(" + ")
47
+ end
48
+
49
+ def exec(args)
50
+ super(args).inject(:+)
51
+ end
52
+ end
53
+
54
+ class Multiply < Expression
55
+ def to_s
56
+ arr = @args.map do |arg|
57
+ str = arg.to_s
58
+ (arg.is_a?(Mathmas::Expression) ? "(" + str + ")" : str)
59
+ end
60
+ arr.join("*")
61
+ end
62
+
63
+ # TODO: (-1)*x -> -x, (-3)*x -> -3x
64
+ def to_tex
65
+ arr = @args.map do |arg|
66
+ str = arg.to_tex
67
+ (arg.is_a?(Mathmas::Expression) ? "(" + str + ")" : str)
68
+ end
69
+ arr.join("*")
70
+ end
71
+
72
+ def exec(args)
73
+ super(args).inject(:*)
74
+ end
75
+ end
76
+
77
+ # @args = [3, 2] -> 3^2
78
+ class Power < Expression
79
+ def to_s
80
+ arr = @args.map do |arg|
81
+ str = arg.to_s
82
+ (arg.is_a?(Mathmas::Expression) ? "(" + str + ")" : str)
83
+ end
84
+ arr.join("^")
85
+ end
86
+
87
+ # TODO: display root when args[1].is_a? Rational
88
+ def to_tex(numerator = Number.new(1))
89
+ strs = @args.map do |arg|
90
+ str = arg.to_tex
91
+ (arg.is_a?(Mathmas::Expression) ? "(" + str + ")" : str)
92
+ end
93
+
94
+ if @args[1].is_a?(Number) && @args[1].num < 0
95
+ denominator = (@args[1].num == -1 ? strs[0] : strs[0] + "^" + (-@args[1].num).to_s)
96
+ "\\frac{#{ numerator.to_tex }}{#{ denominator }}"
97
+ else
98
+ strs.join("^")
99
+ end
100
+ end
101
+
102
+ def exec(args)
103
+ super(args).inject(:**)
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,44 @@
1
+ module Mathmas
2
+ class Function
3
+ include Basic
4
+ attr_reader :name, :vals
5
+
6
+ def initialize(name, vals)
7
+ raise "This is not function" unless vals.all?{|val| val.is_a?(Mathmas::Variable)}
8
+ @expr = nil
9
+ @name = name
10
+ @vals = vals
11
+ Mathmas.add_function self
12
+ end
13
+
14
+ def <=(expr)
15
+ raise "The right hand of Mathmas::Function#<- should be an instance of Mathmas::Basic" unless expr.is_a?(Mathmas::Basic)
16
+ @expr = expr
17
+ end
18
+
19
+ def to_s
20
+ strs = @vals.map{|val| val.to_s}
21
+ @name.to_s + "(#{ strs.join(",") }) = " + @expr.to_s
22
+ end
23
+
24
+ def to_tex
25
+ strs = @vals.map{|val| val.to_s}
26
+ @name.to_s + "(#{ strs.join(",") }) = " + @expr.to_tex
27
+ end
28
+
29
+ # @example
30
+ # f(x) = 1/x
31
+ # f.exec(x: 3) #-> 1/3
32
+ # f.exec(3) #-> 1/3
33
+ #
34
+ def exec(*args)
35
+ if args.length == 1 && args[0].is_a?(Hash)
36
+ @expr.exec(args[0])
37
+ else
38
+ symbols = @vals.map{|val| val.symbol}
39
+ hash = args.zip(symbols).reduce({}){|memo, pair| memo[pair[1]] = pair[0]; memo}
40
+ @expr.exec(hash)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ module Mathmas
2
+ # Internal use only
3
+ # Instantiated by Symbol#coerce or Expression#coerce
4
+ #
5
+ # @example
6
+ # 5*(x + y)
7
+ #
8
+ # x + y is calcurated first and an instance of Expression is created.
9
+ # Then Fixnum#* has no rule when its argument is an instance of Expression so Expression#coerce is called.
10
+ # At last Expression#coerce calls Number#new.
11
+ #
12
+ # Multiply(Number(5), Plus(Symbol(x), Symbol(y))
13
+ #
14
+ class Number
15
+ include Mathmas::Basic
16
+ attr_accessor :num
17
+
18
+ def initialize(num)
19
+ @num = num
20
+ end
21
+
22
+ def to_s
23
+ @num.to_s
24
+ end
25
+
26
+ def to_tex
27
+ @num.to_s
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module Mathmas
2
+ # Class for Symbol like x, y, etc.
3
+ class Variable
4
+ include Mathmas::Basic
5
+ attr_reader :symbol
6
+
7
+ def initialize(symbol)
8
+ @symbol = symbol
9
+ end
10
+
11
+ def to_s
12
+ @symbol.to_s
13
+ end
14
+
15
+ def to_tex
16
+ @symbol.to_s
17
+ end
18
+ end
19
+ end
File without changes
@@ -0,0 +1,33 @@
1
+ module Mathmas
2
+ def method_missing(name, *args)
3
+ if /[a-zA-Z]/ =~ name && name.to_s.length == 1
4
+ if args.length == 0
5
+ if Mathmas.find_function(name).nil?
6
+ return Mathmas::Variable.new(name)
7
+ else
8
+ return Mathmas.find_function(name)
9
+ end
10
+ else
11
+ if args.all? {|arg| arg.is_a?(Numeric)}
12
+ func = Mathmas.find_function(name)
13
+ return func.exec(*args)
14
+ else
15
+ return Mathmas::Function.new(name, args)
16
+ end
17
+ end
18
+ end
19
+ super
20
+ end
21
+
22
+ def add_function(func)
23
+ raise "The first argument should be an instance of Mathmas#Function" unless func.is_a?(Function)
24
+ @@functions[func.name] = func
25
+ end
26
+
27
+ def find_function(name)
28
+ @@functions[name]
29
+ end
30
+
31
+ @@functions = {}
32
+ module_function :add_function, :find_function
33
+ end
@@ -0,0 +1,51 @@
1
+ module Mathmas
2
+ # Mathmas#plot_function plot Mathmas::Function.
3
+ # @example
4
+ # f(x) = 1/x
5
+ # f.plot(func, x: 1..2)
6
+ #
7
+ # g(x, y, a, b) = a*x**2 + b*y**2
8
+ # g.plot(a: 3, b: 3, x: -1..1, y: -1..1)
9
+ #
10
+ def plot_function(func, args={})
11
+ args={div_num: 100}.merge(args)
12
+
13
+ div_num = args[:div_num]
14
+ args.delete :div_num
15
+
16
+ ranges = args.select{|key, val| val.is_a?(Range)}
17
+ numerics = args.select{|key, val| val.is_a?(Numeric)}
18
+
19
+ raise "the number of arguments is wrong" unless ranges.length + numerics.length == func.vals.length
20
+
21
+ case ranges.length
22
+ when 2
23
+ plot = Nyaplot::Plot3D.new
24
+ return plot
25
+ when 1
26
+ plot = Nyaplot::Plot.new
27
+ x_label = ranges.keys[0]
28
+ range = ranges[x_label]
29
+ step = (range.last.to_f - range.begin.to_f)/(div_num-1)
30
+
31
+ x_arr = []; div_num.times {|i| x_arr.push(range.begin + step*i)}
32
+ y_arr = x_arr.map{|x| func.exec({x_label => x})}
33
+
34
+ plot.add(:line, x_arr, y_arr)
35
+ plot.x_label(x_label)
36
+ plot.y_label(func.to_s)
37
+
38
+ return plot
39
+ else
40
+ raise "Nyaplot cannot plot function whose dimention > 3"
41
+ end
42
+ end
43
+
44
+ module_function :plot_function
45
+
46
+ class Function
47
+ def plot(args)
48
+ Mathmas.plot_function(self, args)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,12 @@
1
+ require 'nyaplot'
2
+ require_relative 'function'
3
+
4
+ module Mathmas
5
+ def plot(obj, *args)
6
+ if obj.is_a?(Function)
7
+ Mathmas.plot_function(obj, args[0])
8
+ end
9
+ end
10
+
11
+ module_function :plot
12
+ end
@@ -0,0 +1,3 @@
1
+ module Mathmas
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mathmas/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mathmas"
8
+ spec.version = Mathmas::VERSION
9
+ spec.authors = ["Naoki Nishida"]
10
+ spec.email = ["domitry@gmail.com"]
11
+ spec.summary = %q{Gem for Symbolic mathematics}
12
+ spec.description = %q{Symblic Mathmatics library inspired by SymPy and dxdy}
13
+ spec.homepage = "https://github.com/domitry/mathmas"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "nyaplot", "~> 0.1.1"
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mathmas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Naoki Nishida
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nyaplot
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Symblic Mathmatics library inspired by SymPy and dxdy
56
+ email:
57
+ - domitry@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - examples/Mathmas_develop.ipynb
68
+ - lib/mathmas.rb
69
+ - lib/mathmas/calcs/differentiate.rb
70
+ - lib/mathmas/calcs/integrate.rb
71
+ - lib/mathmas/calcs/minimize.rb
72
+ - lib/mathmas/core/basic.rb
73
+ - lib/mathmas/core/expression.rb
74
+ - lib/mathmas/core/function.rb
75
+ - lib/mathmas/core/number.rb
76
+ - lib/mathmas/core/symbol.rb
77
+ - lib/mathmas/funcs/function.rb
78
+ - lib/mathmas/monkey.rb
79
+ - lib/mathmas/plot/function.rb
80
+ - lib/mathmas/plot/plot.rb
81
+ - lib/mathmas/version.rb
82
+ - mathmas.gemspec
83
+ homepage: https://github.com/domitry/mathmas
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.2.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Gem for Symbolic mathematics
107
+ test_files: []
108
+ has_rdoc: