Module Tsr provides the namespace for the RTensor library. It consists of three classes: Tnsr (which provides the data structure for tensor), Tensor (which is derived from Tnsr and provides mathematical operations), and IRange (which provides integer range with step). It also includes some Global_Variables. And finally it includes module DM just for fun.
TsrTypeSet ==> for Tensor
All the methods are module functions (via module_function).
Shortcuts:
Tsr#l ==> alias for Tsr#linspace
Tsr#r ==> alias for IRange::new
Tsr#t ==> alias for Tensor::new
To create a Tensor of type object with default value of 0 (similar to Matrix):
Tsr#zero ==> yields square 2D
Tsr#identity ==> yields square 2D
Tsr#unit ==> alias for Tsr#identity
Tsr#I ==> alias for Tsr#identity
Tsr#scalar ==> yields square 2D
Tsr#diagonal ==> yields square 2D
Other Tensor creations:
Tsr#linspace ==> yields vector
Tsr#diag ==> yields square 2D
Tsr#join ==> 2D only
Conversion from other data types:
Tsr#from_matrix ==> require ‘matrix’
Tsr#from_narray ==> require ‘narray'
Tsr#from_gsl ==> require ‘gsl’
Graphical representations:
Tsr#gnuplot ==> must install ‘gnuplot’
Symbolic computations:
Object checking:
Tsr#ensure_matrix ==> require ‘matrix’
Specifies all the tensor types available for Tnsr. Currently it consists of
:object, :number, :string, :bool, // just like in _JavaScript_ :char, :uchar, :short, :ushort, :int, :unsigned, :long, :llong, :ullong, :float, :double, :ldouble, :cchar, :cuchar, :cshort, :cushort, :cint, :cunsigned, :clong, :culong, :cllong, :cullong, :cfloat, :cdouble, and :cldouble.
:object implies that the elements are Ruby objects. :number is equivalent to :double (just like in JavaScript). The rest are the names taken from C++ data types where usomething means unsigned something, lsomething means long something, and csomething means complex <something>. Note that there is no :ulong, because it is used for :object.
Specifies all the tensor types available for Tensor. Currently it is the same as those supported in GSL, except that :ulong is used for :object:
:object, :number, :char, :uchar, :short, :ushort, :int, :unsigned, :long, :float, :double, :ldouble, :cfloat, :cdouble, and :cldouble.
Again, :number is equivalent to :double.
Specifies the earliest point in indexing (by default equal to 1 numerically).
Specifies the last point in indexing (equal to -1 numerically).
Specifies that all indices are included in indexing (equal to C++ LONG_MAX numerically).
Alias for: Begin
Alias for: End
Alias for: All
Specifies the version of the RTensor library (rtensor.so).
Specifies the version of the RTensor support library (rtensor_sup.rb).
Similar to Matrix[*rows]. But here the rows do not have to have equal sizes.
Tsr[[25, 93], [-1, 66]]
=>
[ 25 93
-1 66 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_api.rb, line 185
185: def Tsr.[] ( *rows )
186: end
Represents the backward range. It can be considered as IRange with always negative step and the tensor elements in backward order.
b(size = All)
Returns the first size elements in backward order if size is positive, or the last size elements in backward order if size is negative. If size is equal to All or -All then it is equal to (End..-1..Begin). Note that if size is equal to zero, then the range is equal to (End..Begin) which is guaranteed to be empty.
b(start, stop)
Returns the backward range with start start and stop stop with step equal to -1.
b(start, step, stop)
Returns the backward range with start start and stop stop with step step. Note that step cannot be equal to zero and it is always negative.
# File rtensor_sup.rb, line 936
936: def b ( *args ) # => backward IRange
937: case args.size
938: when 0
939: IRange.new(End, 1, Begin, true)
940: when 1
941: size = args.at(0)
942: if size == All || size == -All
943: IRange.new(End, 1, Begin, true)
944: elsif size > 0
945: IRange.new(Begin + size - 1, 1, Begin, true)
946: elsif size < 0
947: IRange.new(End, 1, End + size + 1, true)
948: else
949: IRange.new(End, Begin)
950: end
951: when 2
952: start = args.at(0)
953: stop = args.at(1)
954: IRange.new(start, 1, stop, true)
955: when 3
956: start = args.at(0)
957: step = args.at(1).abs
958: stop = args.at(2)
959: IRange.new(start, -step, stop, true)
960: else
961: raise(ArgumentError, "Wrong number of arguments (#{args.size} for 0..3).")
962: end
963: end
Similar to Matrix::build. But here higher dimensions are supported.
Tsr.build(2, 4) {|row, col| col - row }
=>
[ 0 1 2 3
-1 0 1 2 ]
==> Tensor "object": 2 x 4 = 8 (C; dft = 0)
Tsr.build(3) { rand }
=>
[ 0.4267328343815504 0.2287623771102547 0.8624975594117128
0.968500141296517 0.9169930106632552 0.42811262627229274
0.8244525027362727 0.8535317090260932 0.9954578696315364 ]
==> Tensor "object": 3 x 3 = 9 (C; dft = 0)
# File rtensor_api.rb, line 200
200: def build ( row_size, column_size = row_size, ... )
201: end
Alias for: Complex(…)
# File rtensor_api.rb, line 156
156: def c ( ... )
157: end
Similar to Matrix::column_vector and Matrix::columns. But here the columns do not have to have equal sizes.
Tsr.cols([4, 5, 6])
=>
[ 4
5
6 ]
==> Tensor "object": 3 x 1 = 3 (C; dft = 0)
Tsr.cols([25, 93], [-1, 66])
=>
[ 25 -1
93 66 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_api.rb, line 215
215: def cols ( *columns )
216: end
Returns a diagonal Tensor from an array ary.
Tsr.diag([9, 5, -3])
=>
[ 9 0 0
0 5 0
0 0 -3 ]
==> Tensor "double": 3 x 3 = 9 (C; dft = 0)
# File rtensor_sup.rb, line 974
974: def diag ( ary , type = :-, dft = 0)
975: tsr = Tensor.new([ary.size, ary.size], type, dft) # Form 3
976: unless tsr.row_order?
977: tsr[Tsr.r(Begin, tsr.row_size + 1, End)] = Tensor.new(ary, dft, type)
978: else
979: tsr[Tsr.r(Begin, tsr.col_size + 1, End)] = Tensor.new(ary, dft, type)
980: end
981: tsr
982: end
Similar to Matrix::diagonal.
Tsr.diagonal(9, 5, -3)
=>
[ 9 0 0
0 5 0
0 0 -3 ]
==> Tensor "object": 3 x 3 = 9 (C; dft = 0)
# File rtensor_api.rb, line 225
225: def diagonal ( *values )
226: end
Similar to Matrix::empty. But here it does not provide empty arrays and higher dimensions are supported.
Tsr.empty(2, 0) => [] ==> Tensor "object": 0 x 0 = 0 (C; dft = 0) Tsr.empty(0, 3) => [] ==> Tensor "object": 0 x 0 = 0 (C; dft = 0)
# File rtensor_api.rb, line 237
237: def empty ( row_size = 0, column_size = 0, ... )
238: end
Raises an error if m is not a Matrix.
# File rtensor_sup.rb, line 987
987: def ensure_matrix ( m ) # => nil or error
988: require 'matrix'
989: raise(TypeError, 'The input must be a Matrix.') unless m.kind_of?(Matrix)
990: end
Returns a Tensor from a GSL matrix object.
# File rtensor_sup.rb, line 995
995: def from_gsl ( g ) # => Tensor
996: require 'gsl'
997: g = g + 0
998: if g.class == GSL::Matrix::Complex
999: type = 'cdouble'
1000: elsif g.class == GSL::Matrix::Int
1001: type = 'int'
1002: elsif g.class == GSL::Matrix
1003: type = 'double'
1004: else
1005: raise(TypeError, 'Unknown GSL::Matrix (' + g.class.to_s + ').')
1006: end
1007: dim = g.shape
1008:
1009: if $RowOrder
1010: t = Tensor.new(type.to_sym, dim)
1011: else
1012: $RowOrder = true
1013: t = Tensor.new(type.to_sym, dim)
1014: $RowOrder = false
1015: end
1016: unless t.complex?
1017: (0...t.length).each {|k| t[Begin + k] = g[k]}
1018: else
1019: (0...t.length).each {|k| t[Begin + k] = Complex(g[k].re, g[k].im)}
1020: end
1021: t
1022: end
Returns a Tensor from a Matrix object.
m = Matrix[[25, 93], [-1, 66]]
=> Matrix[[25, 93], [-1, 66]]
Tsr.from_matrix(m)
=>
[ 25 93
-1 66 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_sup.rb, line 1034
1034: def from_matrix ( m ) # => Tensor
1035: ensure_matrix(m)
1036: Tensor.new(m.to_a, 0, :object)
1037: end
Returns a Tensor from an NArray object.
# File rtensor_sup.rb, line 1042
1042: def from_narray ( na ) # => Tensor
1043: require 'narray'
1044: raise(TypeError, 'The input must be an NArray.') unless na.kind_of?(NArray)
1045: case na.typecode
1046: when 1
1047: type = 'uchar'
1048: when 2
1049: type = 'short'
1050: when 3
1051: type = 'int'
1052: when 4
1053: type = 'float'
1054: when 5
1055: type = 'double'
1056: when 6
1057: type = 'cfloat'
1058: when 7
1059: type = 'cdouble'
1060: else
1061: type = 'object'
1062: end
1063: dim = na.shape
1064: dim[0] = na.shape[1]
1065: dim[1] = na.shape[0]
1066:
1067: if $RowOrder
1068: t = Tensor.new(type.to_sym, dim)
1069: else
1070: $RowOrder = true
1071: t = Tensor.new(type.to_sym, dim)
1072: $RowOrder = false
1073: end
1074: (0...t.length).each {|k| t[Begin + k] = na[k]}
1075: t
1076: end
Run the GiNaC Interactive Shell. Type “exit“ to return to Ruby.
# File rtensor_sup.rb, line 1081
1081: def ginsh ( ) # => true or false
1082: system('ginsh.exe')
1083: end
Run the gnuplot command. cmd must be a series of string and they will be separated by spaces. Returns the process ID.
Tsr.gnuplot('plot[][-2:2]', 'sin(x), x, x-x**3/6')
=> 5780
Tsr.gnuplot('plot "C:/prices.txt" u 1:2 w l, "" u 1:3 w lp')
=> 4120
# File rtensor_api.rb, line 176
176: def gnuplot ( *cmd ) # => procID
177: end
Similar to Matrix::identity.
Tsr.identity(2)
=>
[ 1 0
0 1 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_api.rb, line 246
246: def identity ( n )
247: end
Joins several 2D-tensors of the same type into one big tensor. The tensors have to be put into an array ary. The type and default value will follow the first tensor, if any, because the elements of ary can be either a tensor or a scalar. If rowOrd is true then each row must have the same number of sub-rows; if rowOrd is false then each column must have the same number of sub-columns.
Tsr.join([[7, Tsr.s([2, 3, 5])], Tsr.s(:-, [2, 4], 8.9)])
=>
[ 7 2 3 5
8.9 8.9 8.9 8.9
8.9 8.9 8.9 8.9 ]
==> Tnsr "object": 3 x 4 = 12 (C; dft = nil)
# File rtensor_sup.rb, line 1111
1111: def join ( ary, rowOrd = true ) # => new_tsr
1112: case ary
1113: when Array
1114: return Tensor.new if ary.empty?
1115:
1116: is_2D = false
1117: if ary.any? {|e| e.kind_of?(Array)}
1118: is_2D = true
1119: aryFlat = ary.flatten(1)
1120: else
1121: aryFlat = ary
1122: end
1123:
1124: tclass = Tensor
1125: type = 'number'
1126: dft = 0
1127: if aryFlat.any? {|e| e.instance_of?(Tnsr)}
1128: tclass = Tnsr
1129: type = 'object'
1130: dft = nil
1131: end
1132: tsr = aryFlat.detect {|e| e.kind_of?(Tnsr)}
1133: unless tsr.nil?
1134: type = tsr.ttype
1135: dft = tsr.get_dft
1136: end
1137:
1138: if is_2D
1139: if rowOrd
1140: rowCnt = []
1141: colCnt = []
1142: ary.each do |a|
1143: if a.kind_of?(Array)
1144: if a[0].kind_of?(Tnsr)
1145: rowCnt.push(a[0].row_size)
1146: else
1147: rowCnt.push(1)
1148: end
1149:
1150: cCnt = 0
1151: a.each do |t|
1152: if t.kind_of?(Tnsr)
1153: t.ensure_2D
1154: raise(ArgumentError, 'The number of rows must all be the same.') if rowCnt.last != t.row_size
1155: cCnt += t.col_size
1156: else
1157: raise(ArgumentError, 'A scalar cannot be included if rows are more than one.') if rowCnt.last != 1
1158: cCnt += 1
1159: end
1160: end
1161: colCnt.push(cCnt)
1162:
1163: elsif a.kind_of?(Tnsr)
1164: rowCnt.push(a.row_size)
1165: colCnt.push(a.col_size)
1166: else
1167: rowCnt.push(1)
1168: colCnt.push(1)
1169: end
1170:
1171: unless colCnt[2].nil?
1172: raise(ArgumentError, 'The number of cols must all be the same.') if colCnt.last != colCnt[2]
1173: end
1174: end
1175: sum = 0
1176: rowCnt.collect {|i| sum += i}
1177: tsr = tclass.new(type.to_sym, [sum, colCnt.last], dft)
1178:
1179: else # column order
1180: colCnt = []
1181: rowCnt = []
1182: ary.each do |a|
1183: if a.kind_of?(Array)
1184: if a[0].kind_of?(Tnsr)
1185: colCnt.push(a[0].col_size)
1186: else
1187: colCnt.push(1)
1188: end
1189:
1190: rCnt = 0
1191: a.each do |t|
1192: if t.kind_of?(Tnsr)
1193: t.ensure_2D
1194: raise(ArgumentError, 'The number of col must all be the same.') if colCnt.last != t.col_size
1195: rCnt += t.row_size
1196: else
1197: raise(ArgumentError, 'A scalar cannot be included if cols are more than one.') if colCnt.last != 1
1198: rCnt += 1
1199: end
1200: end
1201: rowCnt.push(rCnt)
1202:
1203: elsif a.kind_of?(Tnsr)
1204: colCnt.push(a.col_size)
1205: rowCnt.push(a.row_size)
1206: else
1207: colCnt.push(1)
1208: rowCnt.push(1)
1209: end
1210:
1211: unless rowCnt[2].nil?
1212: raise(ArgumentError, 'The number of rows must all be the same.') if rowCnt.last != rowCnt[2]
1213: end
1214:
1215: end
1216: sum = 0
1217: colCnt.collect {|j| sum += j}
1218: tsr = tclass.new(type.to_sym, [rowCnt.last, sum], dft)
1219: end
1220:
1221: if rowOrd
1222: i = Begin
1223: ary.each do |a|
1224: j = Begin
1225: if a.kind_of?(Array)
1226: a.each do |t|
1227: if t.kind_of?(Tnsr)
1228: tsr[r(i, i + t.row_size - 1), r(j, j + t.col_size - 1)] = t
1229: j += t.col_size
1230: else
1231: tsr[i, j] = t
1232: j += 1
1233: end
1234: end
1235: elsif a.kind_of?(Tnsr)
1236: tsr[r(i, i + a.row_size - 1), r(j, j + a.col_size - 1)] = a
1237: j += a.col_size
1238: else
1239: tsr[i, j] = a
1240: j += 1
1241: end
1242: i += rowCnt.shift
1243: end
1244: else
1245: j = Begin
1246: ary.each do |a|
1247: i = Begin
1248: if a.kind_of?(Array)
1249: a.each do |t|
1250: if t.kind_of?(Tnsr)
1251: tsr[r(i, i + t.row_size - 1), r(j, j + t.col_size - 1)] = t
1252: i += t.row_size
1253: else
1254: tsr[i, j] = t
1255: i += 1
1256: end
1257: end
1258: elsif a.kind_of?(Tnsr)
1259: tsr[r(i, i + a.row_size - 1), r(j, j + a.col_size - 1)] = a
1260: i += a.row_size
1261: else
1262: tsr[i, j] = a
1263: i += 1
1264: end
1265: j += colCnt.shift
1266: end
1267: end
1268: tsr
1269:
1270: else # 1D ----------------------
1271: if rowOrd
1272: if ary[0].kind_of?(Tnsr)
1273: rowCnt = ary[0].row_size
1274: else
1275: rowCnt = 1
1276: end
1277: colCnt = 0
1278: else
1279: rowCnt = 0
1280: if ary[0].kind_of?(Tnsr)
1281: colCnt = ary[0].col_size
1282: else
1283: colCnt = 1
1284: end
1285: end
1286:
1287: ary.each do |t|
1288: if t.kind_of?(Tnsr)
1289: t.ensure_2D
1290: if rowOrd
1291: raise(ArgumentError, 'The number of rows must all be the same.') if rowCnt != t.row_size
1292: colCnt += t.col_size
1293: else
1294: raise(ArgumentError, 'The number of cols must all be the same.') if colCnt != t.col_size
1295: rowCnt += t.row_size
1296: end
1297: else
1298: if rowOrd
1299: raise(ArgumentError, 'A scalar cannot be included if rows are more than one.') if rowCnt != 1
1300: colCnt += 1
1301: else
1302: raise(ArgumentError, 'A scalar cannot be included if cols are more than one.') if colCnt != 1
1303: rowCnt += 1
1304: end
1305: end
1306: end
1307:
1308: tsr = tclass.new(type.to_sym, [rowCnt, colCnt], dft)
1309:
1310: if rowOrd
1311: j = Begin
1312: ary.each do |t|
1313: if t.kind_of?(Tnsr)
1314: tsr[All, r(j, j + t.col_size - 1)] = t
1315: j += t.col_size
1316: else
1317: tsr[j] = t
1318: j += 1
1319: end
1320: end
1321: else
1322: i = Begin
1323: ary.each do |t|
1324: if t.kind_of?(Tnsr)
1325: tsr[r(i, i + t.row_size - 1), All] = t
1326: i += t.row_size
1327: else
1328: tsr[i] = t
1329: i += 1
1330: end
1331: end
1332: end
1333: tsr
1334: end
1335:
1336: when Tnsr
1337: ary[]
1338:
1339: else
1340: Tensor.new(ary)
1341: end
1342: end
Returns a linearly spaced vector of n points between and including the starting point a and the ending point b.
Tsr.linspace(0, 1, 5)
=>
[ 0
0.25
0.5
0.75
1 ]
==> Tensor "double": 5 x 1 = 5 (C; dft = 0)
Also aliased as: Tsr::l
# File rtensor_sup.rb, line 1097
1097: def linspace ( a, b, n = 101, type = :- ) # => Tensor
1098: spc = (b - a).to_f / (n - 1)
1099: Tensor.new([n], type, 0) {|i, m| a + (i - Begin) * spc}
1100: end
Run the gnuplot plot command on the x and y (which can be an Array). Most of args are hashes for gnuplot. Returns the shape of the plot tensor.
Tsr.plot(x = Tsr.l(0, 10), [x.sin, x.cos]) <<<plot "rtensor.txt" u 1:2, "" u 1:3>>> => [101, 3]
# File rtensor_sup.rb, line 1350
1350: def plot ( x, y, *args ) # => array
1351: if y.instance_of?(Array)
1352: Tsr.join([x, *y]).plot(*args)
1353: else
1354: Tsr.join([x, y]).plot(*args)
1355: end
1356: end
Alias for: IRange::new
# File rtensor_api.rb, line 160
160: def r ( ... )
161: end
Similar to Matrix::row_vector and Matrix::rows. But here the rows do not have to have equal sizes and it does not support for copy.
Tsr.rows([4, 5, 6])
=>
[ 4 5 6 ]
==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
Tsr.rows([25, 93], [-1, 66])
=>
[ 25 93
-1 66 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_api.rb, line 259
259: def rows ( *rows )
260: end
Alias for: Tnsr::new
# File rtensor_api.rb, line 164
164: def s ( ... )
165: end
Similar to Matrix::scalar.
Tsr.scalar(2, 5)
=>
[ 5 0
0 5 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_api.rb, line 268
268: def scalar ( n, value )
269: end
Alias for: Tensor::new
# File rtensor_api.rb, line 168
168: def t ( ... )
169: end
Similar to Matrix::zero.
Tsr.zero(2)
=>
[ 0 0
0 0 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_api.rb, line 277
277: def zero ( n )
278: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.