rmatrix 0.1.11 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b03156a99a7338c06f4bde90d9c21af4323e3a75
4
- data.tar.gz: 9c3229920bcb608b8cec92320e2f5c4119d93966
3
+ metadata.gz: e702174b89d1c6001ed7d073f07596c523971e80
4
+ data.tar.gz: 5cde45903463d02075f5980d897770ce302436a4
5
5
  SHA512:
6
- metadata.gz: 2f3d4b98cdb9cb7fde56f80fa4988c9bbf88387c136dd0487ee0192e533d61c0a06a5ed070f6dbdbcc48fa932cf73ab9b216219123e7802ad82a0bce8c6343db
7
- data.tar.gz: 8e19dfaf471c07dbaf8463ab646bb5c9c1f440f2d20ad2f71e8ab4b519f3488d0c2fea61031f7c5f296fff713fb433d82b8d492aafed85d1c453869b881c3bd9
6
+ metadata.gz: 8526932acbe01d0ffc6fc6b0ed58d0a10de481307c956cb1179cfd72b51e937603f7ada7e13dd1fcf1eb5ca7c6ee19b4392bbc6d5b573b8f655d2558a5d63c87
7
+ data.tar.gz: dc8dcd3d0467a29f5deb7dcc0c9a99485454133cba2c206eebd473b2cae8b1f61098abe17424fdbcb5e44a5cb1e930d37c900d673e130680316bcccd2d7b18eb
@@ -58,9 +58,19 @@ module RMatrix
58
58
  narray.to_s << ':' << columns.to_s << ':' << rows.to_s << ':' << narray.typecode.to_s
59
59
  end
60
60
 
61
+ def map(flatten: true)
62
+ narray.to_type(RMatrix::Matrix::Typecode::OBJECT).map do |x|
63
+ yield x
64
+ end.to_a
65
+ end
66
+
67
+ def flat_map(&block)
68
+ map(&block).flatten(1)
69
+ end
70
+
61
71
  def to_f
62
72
  if length === 1
63
- self[0].to_f
73
+ self.narray[0].to_f
64
74
  else
65
75
  raise "Can only call to_f on vectors of length 1"
66
76
  end
@@ -68,11 +78,12 @@ module RMatrix
68
78
 
69
79
  def to_i
70
80
  if length === 1
71
- self[0].to_i
81
+ self.narray[0].to_i
72
82
  else
73
83
  raise "Can only call to_i on vectors of length 1"
74
84
  end
75
85
  end
86
+
76
87
  def self._load arg
77
88
  split_index, buffer, index = 0, '', arg.length - 1
78
89
  split = Array.new(3)
@@ -0,0 +1,84 @@
1
+ class RMatrix::Matrix
2
+ def gplot(type: 'lines', series_names: [], xrange: nil, yrange: nil, title: '', ylabel: '', xlabel: '', style: nil)
3
+ require 'gnuplot'
4
+ require 'base64'
5
+ output_file = Tempfile.new(['plt','.png']).path
6
+ plot = nil
7
+ Gnuplot.open do |gp|
8
+ plot = Gnuplot::Plot.new( gp ) do |plot|
9
+
10
+ plot.xrange "[#{xrange.begin}:#{xrange.end}]" if xrange
11
+ plot.title title
12
+ plot.ylabel ylabel
13
+ plot.xlabel xlabel
14
+ plot.set("style", style) if style
15
+ plot.terminal 'png size 1000,1000'
16
+
17
+ x = (0..50).collect { |v| v.to_f }
18
+ y = x.collect { |v| v ** 2 }
19
+
20
+ plot.output(output_file)
21
+
22
+ plot.data = self.each_row.map.with_index do |row, i|
23
+ Gnuplot::DataSet.new(row.to_a) do |ds|
24
+ ds.with = type
25
+ ds.title = series_names[i] || ''
26
+ yield ds if block_given?
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ puts "\033]1337;File=inline=1:#{Base64.encode64(IO.read(output_file))}\a";
33
+ end
34
+
35
+ def gruff(title: '', type: 'Line', labels:{}, series_names: [], hide_legend: false, theme: Gruff::Themes::RAILS_KEYNOTE)
36
+ require 'gruff'
37
+ g = Gruff.const_get(type).new
38
+ # g.theme = {
39
+ # :colors => ["#3366CC","#DC3912","#FF9900","#109618","#990099","#3B3EAC","#0099C6","#DD4477","#66AA00","#B82E2E","#316395","#994499","#22AA99","#AAAA11","#6633CC","#E67300","#8B0707","#329262","#5574A6","#3B3EAC"],
40
+ # :marker_color => '#dddddd',
41
+ # :font_color => 'black',
42
+ # :background_colors => ['white', '#acacac']
43
+ # # :background_image => File.expand_path(File.dirname(__FILE__) + "/../assets/backgrounds/43things.png")
44
+ # }
45
+ # g.hide_legend = true if hide_legend || series_names.empty? && self.rows.to_i > 10
46
+ # g.title = title
47
+ # g.labels = {}
48
+ self.each_row.map.with_index do |row, i|
49
+ series_name = (series_names[i] || i).to_s
50
+ g.data series_name, row.to_a[0..5].map{|v| v.round(2)}
51
+ end if self.rows
52
+ fname = "/tmp/chrt-#{Random.rand(1000000...9999999)}.png"
53
+ g.write(fname)
54
+ puts "\033]1337;File=inline=1:#{Base64.encode64(g.to_blob)}\a";
55
+ end
56
+
57
+ def threed_gplot(pm3d: false)
58
+ require 'gnuplot'
59
+ output_file = Tempfile.new(['plt','.png']).path
60
+ plot = nil
61
+ Gnuplot.open do |gp|
62
+ plot = Gnuplot::SPlot.new( gp ) do |plot|
63
+
64
+ case IRuby::Kernel.instance
65
+ when nil
66
+ puts "Setting output"
67
+ plot.terminal 'png'
68
+ plot.output(output_file)
69
+ end
70
+ # see sin_wave.rb
71
+ plot.pm3d if pm3d
72
+ plot.hidden3d
73
+
74
+ plot.data << Gnuplot::DataSet.new( self.to_a ) do |ds|
75
+ ds.with = 'lines'
76
+ ds.matrix = true
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ puts "\033]1337;File=inline=1:#{Base64.encode64(IO.read(output_file))}\a";
83
+ end
84
+ end
@@ -0,0 +1,35 @@
1
+ module RMatrix
2
+ module Random
3
+ module ClassMethods
4
+ def normal
5
+ end
6
+
7
+ def uniform(n=10,low: 0.0,high: 1.0)
8
+ M.blank(columns: n).generate do
9
+ ::Random.rand(low..high.to_f)
10
+ end
11
+ end
12
+
13
+ def normal(n=10,low: 0.0,high: 1.0)
14
+ spread = high - low
15
+ M.blank(columns: n, rows: 6).random!.*(spread).+(low).avg(1)
16
+ end
17
+
18
+ def binomial
19
+ end
20
+ end
21
+
22
+ def generate(n=50, initial=nil)
23
+ length.times do |i|
24
+ self[i] = block_given? ?
25
+ yield(i.zero? ? initial : self[i - 1], i, ->(idx){self[idx]}) :
26
+ ::Random.rand(0..10)
27
+ end
28
+ self
29
+ end
30
+
31
+ def self.included(base)
32
+ base.extend ClassMethods
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module RMatrix
2
- VERSION = "0.1.11"
2
+ VERSION = "0.1.13"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wouter Coppieters
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-28 00:00:00.000000000 Z
11
+ date: 2018-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -159,8 +159,10 @@ files:
159
159
  - lib/rmatrix/core.rb
160
160
  - lib/rmatrix/indices.rb
161
161
  - lib/rmatrix/matrix.rb
162
+ - lib/rmatrix/plot.rb
162
163
  - lib/rmatrix/printing/matrix_table.rb
163
164
  - lib/rmatrix/printing/print_table.rb
165
+ - lib/rmatrix/random.rb
164
166
  - lib/rmatrix/shortcuts.rb
165
167
  - lib/rmatrix/typecode.rb
166
168
  - lib/rmatrix/vector.rb