PythonとWolfram Mathematica [Mathematica]
新しい機能
https://reference.wolfram.com/language/WolframClientForPython/index.html
があるようなので、一番簡単な例だけを試してみた。
$ pip3 install wolframclient
Collecting wolframclient
Downloading https://files.pythonhosted.org/packages/2e/01/0d51ce2b1e766acfa17355cc99c7cab2190783542c8ed085260f7c19d8a5/wolframclient-1.0.2-py2.py3-none-any.whl (827kB)
..
略
..
Building wheels for collected packages: aiohttp
Building wheel for aiohttp (PEP 517) ... done
Stored in directory: /Users/yukitoshifujimura/Library/Caches/pip/wheels/f2/8b/d3/ee080552ff95efe1f875d838c77b26127539e6a9e20240937e
Successfully built aiohttp
Installing collected packages: attrs, multidict, idna, yarl, chardet, async-timeout, aiohttp, numpy, pytz, pyzmq, zmq, oauthlib, urllib3, certifi, requests, wolframclient
Running setup.py install for multidict ... done
Running setup.py install for yarl ... done
Running setup.py install for numpy ... done
Running setup.py install for pyzmq ... done
Running setup.py install for zmq ... done
Successfully installed aiohttp-3.5.4 async-timeout-3.0.1 attrs-19.1.0 certifi-2019.3.9 chardet-3.0.4 idna-2.8 multidict-4.5.2 numpy-1.16.2 oauthlib-3.0.1 pytz-2019.1 pyzmq-18.0.1 requests-2.21.0 urllib3-1.24.1 wolframclient-1.0.2 yarl-1.3.0 zmq-0.0.0
——
そうして、
>>> from wolframclient.evaluation import WolframLanguageSession
>>> from wolframclient.language import wl, wlexpr
>>> session = WolframLanguageSession()
>>> session.evaluate(wlexpr('Range[5]'))
[1, 2, 3, 4, 5]
>>> session.evaluate(wl.MinMax([1, -3, 0, 9, 5]))
[-3, 9]
>>> func_squared = wlexpr('#^2 &')
>>> session.evaluate(wl.Map(func_squared, wl.Range(5)))
[1, 4, 9, 16, 25]
のような感じ。
難しい例は
https://mathematica.stackexchange.com/questions/195380/how-can-i-use-the-python-library-networkx-from-mathematica
にある。
https://reference.wolfram.com/language/WolframClientForPython/index.html
があるようなので、一番簡単な例だけを試してみた。
$ pip3 install wolframclient
Collecting wolframclient
Downloading https://files.pythonhosted.org/packages/2e/01/0d51ce2b1e766acfa17355cc99c7cab2190783542c8ed085260f7c19d8a5/wolframclient-1.0.2-py2.py3-none-any.whl (827kB)
..
略
..
Building wheels for collected packages: aiohttp
Building wheel for aiohttp (PEP 517) ... done
Stored in directory: /Users/yukitoshifujimura/Library/Caches/pip/wheels/f2/8b/d3/ee080552ff95efe1f875d838c77b26127539e6a9e20240937e
Successfully built aiohttp
Installing collected packages: attrs, multidict, idna, yarl, chardet, async-timeout, aiohttp, numpy, pytz, pyzmq, zmq, oauthlib, urllib3, certifi, requests, wolframclient
Running setup.py install for multidict ... done
Running setup.py install for yarl ... done
Running setup.py install for numpy ... done
Running setup.py install for pyzmq ... done
Running setup.py install for zmq ... done
Successfully installed aiohttp-3.5.4 async-timeout-3.0.1 attrs-19.1.0 certifi-2019.3.9 chardet-3.0.4 idna-2.8 multidict-4.5.2 numpy-1.16.2 oauthlib-3.0.1 pytz-2019.1 pyzmq-18.0.1 requests-2.21.0 urllib3-1.24.1 wolframclient-1.0.2 yarl-1.3.0 zmq-0.0.0
——
そうして、
>>> from wolframclient.evaluation import WolframLanguageSession
>>> from wolframclient.language import wl, wlexpr
>>> session = WolframLanguageSession()
>>> session.evaluate(wlexpr('Range[5]'))
[1, 2, 3, 4, 5]
>>> session.evaluate(wl.MinMax([1, -3, 0, 9, 5]))
[-3, 9]
>>> func_squared = wlexpr('#^2 &')
>>> session.evaluate(wl.Map(func_squared, wl.Range(5)))
[1, 4, 9, 16, 25]
のような感じ。
難しい例は
https://mathematica.stackexchange.com/questions/195380/how-can-i-use-the-python-library-networkx-from-mathematica
にある。
できる 仕事がはかどるPython自動処理 全部入り。 (「できる全部入り。」シリーズ)
- 作者: 森本哲也
- 出版社/メーカー: インプレス
- 発売日: 2019/05/24
- メディア: 単行本(ソフトカバー)
Introducing Python: Modern Computing in Simple Packages
- 作者: Bill Lubanovic
- 出版社/メーカー: O'Reilly Media
- 発売日: 2019/12/05
- メディア: ペーパーバック
The Student's Introduction to Mathematica and the Wolfram Language
- 作者: Bruce F. Torrence
- 出版社/メーカー: Cambridge University Press
- 発売日: 2019/05/16
- メディア: ペーパーバック