ラベル gnuplot の投稿を表示しています。 すべての投稿を表示
ラベル gnuplot の投稿を表示しています。 すべての投稿を表示

2019年3月6日水曜日

プログラミング言語

プログラミングの周辺の話題で、以前から興味があることがある。

というか、疑問。

「なんであんなにたくさん言語があるのか」




自然言語(英語とか日本語とか中国語とか普通に喋る用の言語)ならわかる。

歴史的要因や地理的要因、流行などその他諸々によって進化していくので、あれだけ数があることも、特別疑問に感じない。

でも、プログラミング言語の場合って、機械語やアセンブリ言語然り、Cなど低いレイヤーの言語があるじゃないですか。なんでそれ一本槍でいかないのかと。

というか、いい年をしてこんなことを疑問に思ってて、でも意外にみんな、そんな感じで素朴に質問されたら答えられないのではないかと思っている。



そこで、

「プログラミング言語はマシンを統制するための言語」

という視点にたち、

「最終的に機械語に置き換えられて実行されるんであれば、程度の差こそあれ『プログラミング言語』という一つのツールとしてみなせないのか」

と思って、いろんな言語同士をお互いに埋め込んだり、処理を受け渡したりする方法をちょこちょこ調べていたりする。

なんか我ながらマニアックなことをやっていると思う。

調べること中心で、あんまり実装はできていないけど、そういうものも含めて興味があることは、
  • pythonコードにC++を埋め込む
  • C++のコードにpythonを埋め込む
  • pythonのコードにJavaを埋め込む
  • Javaのコードにpythonを埋め込む(←あんまりやる意味ない?)
  • Javascriptの中にC++を埋め込む
などなど以下、他の言語同士でも同様。さらに、
  • C++のコードにアセンブリ言語を埋め込む
  • SVGの中にC++を埋め込む
  • C++からmatplotlibへ描画
  • C++からgnuplotへ描画
  • ProcessingにC++を埋め込む
などなど。あんまり理解していないので、そもそもやる意味ないやつとか、支離滅裂なことをいっている組み合わせもあるかも。




自然言語で話すときは、方言とか、違う言語通しチャンポンで話すことも結構あったりするのに、プログラミング言語でそれができないのは、なんかもどかしいなっていう感じ。

なんか発言がど素人的な発言だけど。

でもいいのさ。素人の目線でみて、専門家的に考え検討するの大事だと思うから気にしなーい!

2016年7月10日日曜日

シグモイド関数の描画

gnuplotもインストールしたし、matplotlibも使える状態だしということで、なんだかんだ可視化やグラフ描画をするツールは割りと揃ってきた。。

それぞれ、ツールの使い方の勉強という意味含め、ニューラルネットなどででてくるのシグモイド関数の振る舞いをイメージしておきたいなども理由もあるので、シグモイド関数を描画してみようと思い描画してみた。。

gnuplotでの描画

gnuplotでもfor文が使えるらしいのでそれを使う。

ターミナルを起動して、gnuplotを起動する。
hiroshi-no-MacBook-Air:Pro-tr hiroshi$ gnuplot     #gnuplotと打ち込んでreturnを押す
起動したら次のように打ちます。
gnuplot> set yrange[0:1]       #見栄えを良くするための設定。y軸方向の描画範囲の指定。
gnuplot> plot for [i=0:10] 1/(1+exp(-i*x/10))
これを実行すると、次のような図が表示される。




gnuplotを起動したときに、毎回表示されるバージョンを確認しよう。
どうやら、plot forという書き方はバージョン4.4からの機能のようだ。
(参考:明日できること)

matplotlibでの描画

matplotlibでは次のように描画する。

matplotlibとscipyがインストールできていることは前提。 インストールできていない場合はまずこちらから。
Macでpython3の環境構築9---Scipyのインストール---
Macでpython3の環境構築4---Matplotlibのインストール1---
from matplotlib import pyplot as plt
import scipy as sp

x=sp.arange(-10,10,0.1)
for t in range(10):        #必要なグラフは先にすべて生成する。
  y=1/(1+sp.exp(-(2*t*x/10)))
  plt.plot(x,y)         


plt.show()   #最後にまとめて描画すると重ねて表示される。
matplotlibの使い方の説明にもなってしまうが、グラフを重ねて描画するときは、先にplt.plot()、plt.plot()という感じで必要なグラフをすべて生成して、最後にplt.show()をして最後に描画する。

上記を例えば、テキストファイルに書き、sigmoid.pyという名前で保存。sigmoid.pyがあるカレントディレクトリに移動して、
hiroshi-no-MacBook-Air:Pro-tr hiroshi$ python3 sigmoid.py
とコマンドを打って実行すると、次のような画面が出てくる。



for文のところで、あまり数を刻みすぎてしまうと、グラフが表示されたときよくわからない表示になってしまうので、適当に間引いて描画したほうがそれらしくみえる。

参考にしたページや文献・書籍
明日できること
シグモイド関数---wiki

2016年6月22日水曜日

gnuplotのインストール

ここ最近いろいろなものをインストールしております。

前回はTeXをインストールしましたが、gnuplotもインストールしました。

これについてもインストール方法を残しておこうと思います。


Aquatermのインストール

やや天下り的な感じになってしまいますが、gnuplotだけインストールしても出力をする先がないので、描画ソフトもインストールする必要があります。
調べた感じでは、ここで書くAquatermと次で書くX11が代表的なもののようなので、それをインストールします。

Aquatermのインストールについてはまずダウンロードのサイトへアクセスします。

ダウンロードしたファイルを実行すると、インストールの画面が立ち上がります。

右下の「続ける」ボタンを押して次へ行きます。そのあとも画面の指示通りに進んで行きます。

続けるを押すと同意するかどうかを聞かれますので、同意をして先へ進みます。


「インストール」ボタンを押すと、いろいろ処理が始まり、最終的には次の画面になります。
Finderのアプリケーションのディレクトリをみると、確かにAquatermがインストールされています。


X11(Xquarz)のインストール

いろいろ調べてみると、現在はX11はXquarzは統合されているようです。
これもインストール方法は、ほとんどAquatermを同じです。

まずはダウンロードサイトへアクセスします。

ファイルをダウンロードして実行すると、インストール画面が立ち上がりますので、それに従って進んでいくだけです。







インストール完了の画面のスクリーンショットと撮り忘れましたが、最後にインストール完了の画面が表示されます。

でFinder中のアプリケーションの中のユーティリティの中をみるとXQuarzがインストールされています。

gnuplotのインストール

ここまでやってしまえば、あとはすぐです。 ターミナルを起動して次のようにコマンドを打ちます。
hiroshi-no-MacBook-Air:~ hiroshi$ sudo brew install gnuplot --with-aquaterm

そうすると、次のような処理が始まります。

==> Installing dependencies for gnuplot: pkg-config, libpng, freetype, fo
==> Installing gnuplot dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.el_capitan.
######################################################################## 100.0%
==> Pouring pkg-config-0.29.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29: 10 files, 624.4K
==> Installing gnuplot dependency: libpng
==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.21.el_capitan.bo
######################################################################## 100.0%
==> Pouring libpng-1.6.21.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libpng/1.6.21: 17 files, 1.2M
==> Installing gnuplot dependency: freetype
==> Downloading https://homebrew.bintray.com/bottles/freetype-2.6.3.el_capitan.b
######################################################################## 100.0%
==> Pouring freetype-2.6.3.el_capitan.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/freetype-config
Target /usr/local/bin/freetype-config
already exists. You may want to remove it:
  rm '/usr/local/bin/freetype-config'

To force the link and overwrite all conflicting files:
  brew link --overwrite freetype

To list all files that would be deleted:
  brew link --overwrite --dry-run freetype

Possible conflicting files are:
/usr/local/bin/freetype-config
/usr/local/include/freetype2/ft2build.h
/usr/local/share/aclocal/freetype2.m4
/usr/local/share/man/man1/freetype-config.1
/usr/local/lib/libfreetype.dylib -> /usr/local/lib/libfreetype.6.dylib
==> Summary
🍺  /usr/local/Cellar/freetype/2.6.3: 60 files, 2.5M
==> Installing gnuplot dependency: fontconfig
==> Downloading https://homebrew.bintray.com/bottles/fontconfig-2.11.1_2.el_capi
######################################################################## 100.0%
==> Pouring fontconfig-2.11.1_2.el_capitan.bottle.tar.gz
==> /usr/local/Cellar/fontconfig/2.11.1_2/bin/fc-cache -frv
Last 15 lines from /Users/hiroshi/Library/Logs/Homebrew/fontconfig/01.fc-cache:
2016-05-29 23:47:46 +0900

/usr/local/Cellar/fontconfig/2.11.1_2/bin/fc-cache
-frv

dyld: Library not loaded: /usr/local/opt/freetype/lib/libfreetype.6.dylib
  Referenced from: /usr/local/Cellar/fontconfig/2.11.1_2/bin/fc-cache
  Reason: image not found
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall fontconfig`
==> Summary
🍺  /usr/local/Cellar/fontconfig/2.11.1_2: 449 files, 2.9M
==> Installing gnuplot dependency: libtiff
==> Downloading https://homebrew.bintray.com/bottles/libtiff-4.0.6.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libtiff-4.0.6.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libtiff/4.0.6: 259 files, 3.4M
==> Installing gnuplot dependency: gd
==> Downloading https://homebrew.bintray.com/bottles/gd-2.1.1_2.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gd-2.1.1_2.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/gd/2.1.1_2: 34 files, 1M
==> Installing gnuplot dependency: lua
==> Downloading https://homebrew.bintray.com/bottles/lua-5.2.4_3.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring lua-5.2.4_3.el_capitan.bottle.tar.gz
==> Caveats
Please be aware due to the way Luarocks is designed any binaries installed
via Luarocks-5.2 AND 5.1 will overwrite each other in /usr/local/bin.

This is, for now, unavoidable. If this is troublesome for you, you can build
rocks with the `--tree=` command to a special, non-conflicting location and
then add that to your `$PATH`.
==> Summary
🍺  /usr/local/Cellar/lua/5.2.4_3: 82 files, 687.2K
==> Installing gnuplot
==> Downloading https://downloads.sourceforge.net/project/gnuplot/gnuplot/5.0.2/gnuplot-5.0.2.tar.gz
==> Downloading from http://tenet.dl.sourceforge.net/project/gnuplot/gnuplot/5.0.2/gnuplot-5.0.2.tar.gz
######################################################################## 100.0%
==> ./configure --disable-silent-rules --prefix=/usr/local/Cellar/gnuplot/5.0.2 --with-readline=/usr/local/opt/readline --with-
==> make
==> make install
==> Caveats
AquaTerm support will only be built into Gnuplot if the standard AquaTerm
package from SourceForge has already been installed onto your system.
If you subsequently remove AquaTerm, you will need to uninstall and then
reinstall Gnuplot.
==> Summary
🍺  /usr/local/Cellar/gnuplot/5.0.2: 44 files, 2.2M, built in 2 minutes 26 seconds
hiroshi-no-MacBook-Air:~ hiroshi$


これでインストールは完了です。

確認として、次のようになれば問題なしです。

hiroshi-no-MacBook-Air:~ hiroshi$ gnuplot

G N U P L O T
Version 5.0 patchlevel 2    last modified 2015-12-24

Copyright (C) 1986-1993, 1998, 2004, 2007-2015
Thomas Williams, Colin Kelley and many others

gnuplot home:     http://www.gnuplot.info
faq, bugs, etc:   type "help FAQ"
immediate help:   type "help"  (plot window: hit 'h')

Terminal type set to 'aqua'
gnuplot>

最後の行で、

gnuplot> plot sin(x)

と打てば、また別に画面が立ち上がり、


その他

このままでは、せっかくX11もインストールしたのに、出力先としてX11がつかえません。
本当はオプションを指定する際にX11も一緒に指定しなければならないようですが、間違ってAquatermだけ指定してしまいました。

改めてX11も指定する方法については、またの機会にします。。。