RUBY_VERSION
"2.4.0"
IRuby::VERSION
"0.3"
require 'numo/narray'
require 'numo/gnuplot'
true
# データの作成
x = Numo::DFloat.new(60).seq(0, 0.1) # 0から6まで0.1刻みで生成
y1 = Numo::DFloat::Math.sin(x)
y2 = Numo::DFloat::Math.cos(x)
nil
x.to_a
[0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1.0, 1.1, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5, 1.6, 1.7000000000000002, 1.8, 1.9000000000000001, 2.0, 2.1, 2.2, 2.3000000000000003, 2.4000000000000004, 2.5, 2.6, 2.7, 2.8000000000000003, 2.9000000000000004, 3.0, 3.1, 3.2, 3.3000000000000003, 3.4000000000000004, 3.5, 3.6, 3.7, 3.8000000000000003, 3.9000000000000004, 4.0, 4.1000000000000005, 4.2, 4.3, 4.4, 4.5, 4.6000000000000005, 4.7, 4.800000000000001, 4.9, 5.0, 5.1000000000000005, 5.2, 5.300000000000001, 5.4, 5.5, 5.6000000000000005, 5.7, 5.800000000000001, 5.9]
y1.to_a
[0.0, 0.09983341664682815, 0.19866933079506122, 0.2955202066613396, 0.3894183423086505, 0.479425538604203, 0.5646424733950355, 0.6442176872376911, 0.7173560908995228, 0.7833269096274834, 0.8414709848078965, 0.8912073600614354, 0.9320390859672264, 0.963558185417193, 0.9854497299884603, 0.9974949866040544, 0.9995736030415051, 0.9916648104524686, 0.9738476308781951, 0.9463000876874145, 0.9092974268256817, 0.8632093666488737, 0.8084964038195901, 0.74570521217672, 0.6754631805511506, 0.5984721441039565, 0.5155013718214642, 0.4273798802338298, 0.33498815015590466, 0.23924932921398198, 0.1411200080598672, 0.04158066243329049, -0.058374143427580086, -0.15774569414324865, -0.25554110202683167, -0.35078322768961984, -0.44252044329485246, -0.5298361409084934, -0.6118578909427193, -0.6877661591839741, -0.7568024953079282, -0.8182771110644108, -0.8715757724135882, -0.9161659367494549, -0.951602073889516, -0.977530117665097, -0.9936910036334645, -0.9999232575641008, -0.9961646088358406, -0.9824526126243325, -0.9589242746631385, -0.9258146823277321, -0.8834546557201531, -0.8322674422239008, -0.7727644875559871, -0.7055403255703919, -0.6312666378723208, -0.5506855425976376, -0.4646021794137566, -0.373876664830236]
y2.to_a
[1.0, 0.9950041652780258, 0.9800665778412416, 0.955336489125606, 0.9210609940028851, 0.8775825618903728, 0.8253356149096782, 0.7648421872844884, 0.6967067093471654, 0.6216099682706644, 0.5403023058681398, 0.4535961214255773, 0.3623577544766734, 0.26749882862458735, 0.16996714290024081, 0.0707372016677029, -0.029199522301288815, -0.12884449429552486, -0.2272020946930871, -0.3232895668635036, -0.4161468365471424, -0.5048461045998576, -0.5885011172553458, -0.6662760212798244, -0.7373937155412458, -0.8011436155469337, -0.8568887533689473, -0.9040721420170612, -0.9422223406686583, -0.9709581651495907, -0.9899924966004454, -0.9991351502732795, -0.9982947757947531, -0.9874797699088649, -0.9667981925794609, -0.9364566872907963, -0.896758416334147, -0.848100031710408, -0.7909677119144165, -0.7259323042001399, -0.6536436208636119, -0.5748239465332685, -0.4902608213406994, -0.40079917207997545, -0.30733286997841935, -0.2107957994307797, -0.11215252693505398, -0.01238866346289056, 0.08749898343944727, 0.18651236942257576, 0.28366218546322625, 0.37797774271298107, 0.4685166713003771, 0.5543743361791615, 0.6346928759426347, 0.70866977429126, 0.7755658785102502, 0.8347127848391598, 0.8855195169413194, 0.9274784307440359]
# グラフの描画
g = Numo.noteplot do
# set term: {png: {size: [640, 480]}} # 画像サイズ
# set output: 'ruby_graph.png'
set title: 'sin & cos' # タイトル
set key: 'box left bottom'
set offset: [0, 0, 0, 0]
plot x, y1, {w: 'lines', lw: 3, title: 'sin'},
x, y2, {w: 'lines', lw: 3, title: 'cos'}
end
def AND(x1, x2)
x = Numo::DFloat[x1, x2]
w = Numo::DFloat[0.5, 0.5] # 重み
b = -0.7 # バイアス
tmp = (w*x).sum + b
if tmp <= 0
0
else
1
end
end
:AND
[[0, 0], [1, 0], [0, 1], [1, 1]].each do |x1, x2|
y = AND(x1, x2)
puts "(#{x1}, #{x2}) -> #{y}"
end
(0, 0) -> 0 (1, 0) -> 0 (0, 1) -> 0 (1, 1) -> 1
[[0, 0], [1, 0], [0, 1], [1, 1]]
def NAND(x1, x2)
x = Numo::DFloat[x1, x2]
w = Numo::DFloat[-0.5, -0.5]
b = 0.7
tmp = (w*x).sum + b
if tmp <= 0
0
else
1
end
end
:NAND
[[0, 0], [1, 0], [0, 1], [1, 1]].each do |x1, x2|
y = NAND(x1, x2)
puts "(#{x1}, #{x2}) -> #{y}"
end
(0, 0) -> 1 (1, 0) -> 1 (0, 1) -> 1 (1, 1) -> 0
[[0, 0], [1, 0], [0, 1], [1, 1]]
def OR(x1, x2)
x = Numo::DFloat[x1, x2]
w = Numo::DFloat[0.5, 0.5]
b = -0.2
tmp = (w*x).sum + b
if tmp <= 0
0
else
1
end
end
:OR
[[0, 0], [1, 0], [0, 1], [1, 1]].each do |x1, x2|
y = OR(x1, x2)
puts "(#{x1}, #{x2}) -> #{y}"
end
(0, 0) -> 0 (1, 0) -> 1 (0, 1) -> 1 (1, 1) -> 1
[[0, 0], [1, 0], [0, 1], [1, 1]]
def XOR(x1, x2)
s1 = NAND(x1, x2)
s2 = OR(x1, x2)
y = AND(s1, s2)
return y
end
:XOR
[[0, 0], [1, 0], [0, 1], [1, 1]].each do |x1, x2|
y = XOR(x1, x2)
puts "(#{x1}, #{x2}) -> #{y}"
end
(0, 0) -> 0 (1, 0) -> 1 (0, 1) -> 1 (1, 1) -> 0
[[0, 0], [1, 0], [0, 1], [1, 1]]