Tensor is derived from Tnsr. It supports many more mathematical operations and therefore its supported types are much more limited. Currently the types are those that are supported in GSL, except that :ulong is used for :object. The types are
Ruby object and number
char, uchar, short, ushort, int, unsigned, and long (for the Integer types; but note that there is no ulong, because it is used for Ruby object)
float, double, and ldouble (for the Float types)
cfloat, cdouble, and cldouble (for the corresponding Complex Float types)
Also, the default type $TsrType is :number (which is equivalent to :double) and the default value is 0 (zero).
To create a Tensor :
Tsr#t ==> alias for Tensor::new
Properties of Tensor :
Tensor#singular? ==> must be square 2D
Tensor operations:
- (subtraction)
* (matrix multiplication)
/ (division)
% (modulo division)
** (matrix exponentiation) ==> must be square 2D
Tensor#pow (element-by-element exponentiation)
Tensor#atan2 (element-by-element atan2)
& (bitwise AND)
| (bitwise OR)
^ (bitwise XOR)
<< (left bitshift)
>> (right bitshift)
== (element-by-element equal)
!= (element-by-element not equal)
< (element-by-element less than)
> (element-by-element less than)
<= (element-by-element less than or equal to)
>= (element-by-element greater than or equal to)
Tensor self operations:
Tensor#sub! ( -= )
Tensor#div! ( /= )
Tensor#mod! ( %= )
Tensor#and! ( &= )
Tensor#or! ( |= )
Tensor#xor! ( ^= )
Tensor#lbs! ( <<= )
Tensor#rbs! ( >>= )
Tensor functions:
Tensor mathematical functions:
Tensor matrix functions:
Tensor#determinant ==> must be square 2D
Tensor#det ==> alias for Tensor#determinant
Tensor#cofactor ==> must be square 2D
Tensor#adjoint ==> must be square 2D
Tensor#adj ==> alias for Tensor#adjoint
Tensor#inverse ==> must be square 2D
Tensor#inv ==> alias for Tensor#inverse
Conversion to other data types:
Tensor#to_narray ==> require ‘narray’
Tensor#to_gsl ==> require ‘gsl’, must be 2D
String representations:
Graphical representations:
Tensor#plot ==> must install ‘gnuplot’
Returns a bool tensor by performing element-by-element not equal operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is not equal by t. The default value and sparseness follow the tensor on the left of the != sign. This operation is undefined for Complex types.
Tsr[7, 3.5, -4.1] != Tsr[7, 5, -8] => [ false true true ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0) Tsr[7, 3.5, -4.1] != 7 => [ false true true ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1272
1272: def != ( t ) # => bool_tnsr
1273: end
Returns a new tensor by performing element-by-element modulo operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is modulo divided by t. The default value and sparseness follow the tensor on the left of the % sign. This operation is undefined for Float and Complex types.
Tsr[[7, 6], [3, 9]] % Tsr[[2, 9], [3, 1]]
=>
[ 1 6
0 0 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[7, 6], [3, 9]] % 2.0
=>
[ 1.0 0.0
1.0 1.0 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
For element-by-element divison, see Tensor #/.
# File rtensor_api.rb, line 1183
1183: def % ( t ) # => new_tensor
1184: end
Returns a new tensor by performing element-by-element bitwise AND operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is bitwise AND by t. The default value and sparseness follow the tensor on the left of the & sign. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8] & Tsr[5, 3, 4] => [ 4 3 0 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8] & 5 => [ 4 5 0 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element bitwise OR, see Tensor #|.
# File rtensor_api.rb, line 1196
1196: def & ( t ) # => new_tensor
1197: end
Returns a new tensor by performing matrix multiplication. If t is a tensor, then currently the two tensors have to be matrices (2-D), the second dimension of the left tensor has to be equal to the first dimension of the right tensor, and currently they have to be of exactly the same type; else each element is multiplied by t. The default value and sparseness follow the tensor on the left of the * sign.
Tsr[[2, 4], [6, 8]] * Tsr.identity(2)
=>
[ 2 4
6 8 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[2, 4], [6, 8]] * 2
=>
[ 4 8
12 16 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
For element-by-element multiplication, see Tnsr #*.
# File rtensor_api.rb, line 1153
1153: def * ( t ) # => new_tensor
1154: end
Returns a new tensor by performing exponentiation of the square 2D-tensor (matrix). The input k has to be Integer.
Tsr[[7, 6], [3, 9]] ** 2
=>
[ 67 96
48 99 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
# File rtensor_sup.rb, line 411
411: def ** ( k ) # => new_tsr
412: ensure_square
413: raise(ArgumentError, 'The exponent has to be Integer.') unless k.kind_of?(Integer)
414: if k > 0
415: prod = self[]
416: (2..k).each {prod *= self}
417: prod
418: elsif k < 0
419: p = inverse
420: prod = p
421: (2..-k).each {prod *= p}
422: prod
423: else
424: Tsr.identity(row_size)
425: end
426: end
Returns a new tensor by performing element-by-element subtraction. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is subtracted by t. The default value and sparseness follow the tensor on the left of the - sign.
Tsr[[1, 5], [4, 2]] - Tsr[[9, 3], [-4, 1]]
=>
[ -8 2
8 1 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[1, 5], [4, 2]] - 3
=>
[ -2 2
1 -1 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
For element-by-element addition, see Tnsr #+.
# File rtensor_api.rb, line 1138
1138: def - ( t ) # => new_tensor
1139: end
Returns a new tensor by performing element-by-element division. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is divided by t. The default value and sparseness follow the tensor on the left of the / sign. (Note that “/“ for Integers implies integer divison.)
Tsr[[7, 6], [3, 9]] / Tsr[[2, 9], [3, 1]]
=>
[ 3 0
1 9 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[7, 6], [3, 9]] / 2.0
=>
[ 3.5 3.0
1.5 4.5 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
For element-by-element multiplication, see Tnsr #*.
# File rtensor_api.rb, line 1168
1168: def / ( t ) # => new_tensor
1169: end
Returns a bool tensor by performing element-by-element less than operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is less than by t. The default value and sparseness follow the tensor on the left of the < sign. This operation is undefined for Complex types.
Tsr[7, 3.5, -4.1] < Tsr[7, 5, -8] => [ false true false ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0) Tsr[7, 3.5, -4.1] < 7 => [ false true true ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1284
1284: def < ( t ) # => bool_tnsr
1285: end
Returns a new tensor by performing element-by-element left bitshift operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is left bitshift by t. The default value and sparseness follow the tensor on the left of the << sign. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8] << Tsr[2, 3, 1] => [ 16 56 16 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8] << 2 => [ 16 28 32 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element right bitshift, see Tensor #>>.
# File rtensor_api.rb, line 1235
1235: def << ( t ) # => new_tensor
1236: end
Returns a bool tensor by performing element-by-element less than or equal to operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is less than or equal to by t. The default value and sparseness follow the tensor on the left of the <= sign. This operation is undefined for Complex types.
Tsr[7, 3.5, -4.1] <= Tsr[7, 5, -8] => [ true true false ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0) Tsr[7, 3.5, -4.1] <= 7 => [ true true true ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1308
1308: def <= ( t ) # => bool_tnsr
1309: end
Returns a bool tensor by performing element-by-element equal operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is equal by t. The default value and sparseness follow the tensor on the left of the == sign. This operation is undefined for Complex types.
Tsr[7, 3.5, -4.1] == Tsr[7, 5, -8] => [ true false false ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0) Tsr[7, 3.5, -4.1] == 7 => [ true false false ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1260
1260: def == ( t ) # => bool_tnsr
1261: end
Returns a bool tensor by performing element-by-element greater than operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is greater than by t. The default value and sparseness follow the tensor on the left of the > sign. This operation is undefined for Complex types.
Tsr[7, 3.5, -4.1] > Tsr[7, 5, -8] => [ false false true ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0) Tsr[7, 3.5, -4.1] > 7 => [ false false false ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1296
1296: def > ( t ) # => bool_tnsr
1297: end
Returns a bool tensor by performing element-by-element greater than or equal to operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is greater than or equal to by t. The default value and sparseness follow the tensor on the left of the >= sign. This operation is undefined for Complex types.
Tsr[7, 3.5, -4.1] >= Tsr[7, 5, -8] => [ true false true ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0) Tsr[7, 3.5, -4.1] >= 7 => [ true false false ] ==> Tnsr "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1320
1320: def >= ( t ) # => bool_tnsr
1321: end
Returns a new tensor by performing element-by-element right bitshift operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is right bitshift by t. The default value and sparseness follow the tensor on the left of the >> sign. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8] >> Tsr[2, 3, 1] => [ 1 0 4 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8] >> 2 => [ 1 1 2 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element left bitshift, see Tensor #<<.
# File rtensor_api.rb, line 1248
1248: def >> ( t ) # => new_tensor
1249: end
Returns a new tensor by performing element-by-element bitwise exclusive OR operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is bitwise exclusive OR by t. The default value and sparseness follow the tensor on the left of the ^ sign. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8] ^ Tsr[5, 3, 4] => [ 1 4 12 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8] ^ 5 => [ 1 2 13 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element bitwise OR, see Tensor #|.
# File rtensor_api.rb, line 1222
1222: def ^ ( t ) # => new_tensor
1223: end
Returns a new tensor by performing element-by-element acos operation. This operation is undefined for Complex types.
Tsr[0, 0.5, 1].acos => [ 1.5707963267948966 1.0471975511965979 0.0 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1463
1463: def acos ( ) # => new_tensor
1464: end
Returns a new tensor which is the adjacent difference of the original tensor and the order is determined by Tnsr#row_order? and it preserves this row-orderness. Given a sequence a, b, c, d, etc., adjacent_diff produces a, b-a, c-b, d-c, etc. If the symbol sym is given, it utilizes only Ruby (and not C++) functions and it produces a, b sym a, c sym b, d sym c, etc.
Tsr.t([17, 19, 20, 20, 17]).adjacent_diff => [ 17 2 1 0 -3 ] ==> Tensor "double": 1 x 5 = 5 (C; dft = 0) Tsr.t([1, 2, 6, 24]).adjacent_diff(:/) => [ 1 2 3 4 ] ==> Tensor "double": 1 x 4 = 4 (C; dft = 0)
See also Tnsr#partial_sum which is the inverse.
# File rtensor_api.rb, line 1572
1572: def adjacent_diff ( sym = :- ) # => new_tensor
1573: end
Returns the adjoint of the square 2D-tensor (matrix). The adjoint is defined as the transpose of the matrix of cofactors.
Tsr[[3, 2, -1], [1, 6, 3], [2, -4, 0]].adjoint
=>
[ 12 4 12
6 2 -10
-16 16 16 ]
==> Tensor "object": 3 x 3 = 9 (C; dft = 0)
# File rtensor_sup.rb, line 437
437: def adjoint ( ) # => new_tsr
438: ensure_square
439: tt = collect(true) {|e, i, m| cofactor(m[0], m[1])}
440: tt.transpose
441: end
Returns self after performing element-by-element bitwise AND operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is bitwise AND by t. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8].and!(Tsr[5, 3, 4]) => [ 4 3 0 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8].and!(5) => [ 4 5 0 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element bitwise OR, see Tensor#or!.
# File rtensor_api.rb, line 1403
1403: def and! ( t ) # => self
1404: end
Returns a new tensor by performing element-by-element asin operation. This operation is undefined for Complex types.
Tsr[0, 0.5, 1].asin => [ 0.0 0.5235987755982989 1.5707963267948966 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1471
1471: def asin ( ) # => new_tensor
1472: end
Returns a new tensor by performing element-by-element atan operation. This operation is undefined for Complex types.
Tsr[0, 0.5, 1].atan => [ 0.0 0.4636476090008061 0.7853981633974483 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1479
1479: def atan ( ) # => new_tensor
1480: end
Returns a new tensor by performing element-by-element atan2 operation. (atan2 is arctan(y, x).) If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is atan2 by t. The default value and sparseness follow the tensor on the left of the atan2 sign. This operation is undefined for Complex types.
Tsr[2, 3, 4].atan2(Tsr[4, 2, 3]) => [ 0.4636476090008061 0.982793723247329 0.9272952180016122 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[1, 2, 3].atan2(1) => [ 0.7853981633974483 1.1071487177940904 1.2490457723982544 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1345
1345: def atan2 ( t ) # => new_tensor
1346: end
Returns the cofactor of the i, j element of the square 2D-tensor (matrix). The cofactor is defined as as (-1)**(i + j) * Tensor#determinant of the (i, j) Tnsr#minor.
Tsr[[3, 1, -4], [2, 5, 6], [1, 4, 8]].cofactor(Tsr::End, Tsr::Begin + 1) => -26
# File rtensor_sup.rb, line 450
450: def cofactor ( i, j ) # => numeric
451: ensure_square
452: fac = (1)**(i + j) * minor(i, j).det
453: end
Returns a new tensor by performing element-by-element cos operation.
Tsr[0, 1, 2].cos => [ 1.0 0.5403023058681398 -0.4161468365471424 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1487
1487: def cos ( ) # => new_tensor
1488: end
Returns a new tensor by performing element-by-element cosh operation.
Tsr[0, 1, 2].cosh => [ 1.0 1.5430806348152437 3.7621956910836314 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1495
1495: def cosh ( ) # => new_tensor
1496: end
Returns the determinant of the square 2D-tensor (matrix). This is based on the cofactor expansion along the first row (and therefore it will have exponential growth in the number of elements).
Tsr[[7, 6], [3, 9]].determinant => 45
# File rtensor_sup.rb, line 460
460: def determinant ( ) # => numeric
461: ensure_square
462: return self[Begin] if length == 1
463:
464: # Cofactor expansion along the first row
465: sum = 0
466: self.row(Begin).collect(true) {|e, i, m| sum += e * cofactor(m[0], m[1])}
467: sum
468: end
Returns self after performing element-by-element division. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is divided by t. (Note that “div!“ for Integers implies integer divison.)
Tsr[[7, 6], [3, 9]].div!(Tsr[[2, 9], [3, 1]])
=>
[ 3 0
1 9 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[7, 6], [3, 9]].div!(2.0)
=>
[ 3.5 3.0
1.5 4.5 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
For element-by-element multiplication, see Tnsr#mul!.
# File rtensor_api.rb, line 1375
1375: def div! ( t ) # => self
1376: end
Returns a new tensor by performing element-by-element exp operation.
Tsr[0, 1, 2].exp => [ 1.0 2.718281828459045 7.38905609893065 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1503
1503: def exp ( ) # => new_tensor
1504: end
(TBD: Will cause the Interactive Ruby to behave strangely.)
# File rtensor_sup.rb, line 475
475: def ginac ( arg = '' )
476: ensure_2D
477: g = IO.popen('ginsh.exe', 'w+')
478: g.puts(arg.to_s + ';')
479: g.close_write
480: g.gets
481: end
Returns a new tensor which is the inverse of the square 2D-tensor (matrix) or nil if the matrix is singular. This is based on the matrix of cofactors (and therefore it will have exponential growth in the number of elements).
Tsr[[-1, -1], [0, -1]].inv
=>
[ -1.0 1.0
0.0 -1.0 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[3, 2, -1], [1, 6, 3], [2, -4, 0]].inv.map(&:to_r)
=>
[ (3/16) (1/16) (3/16)
(3/32) (1/32) (-5/32)
(-1/4) (1/4) (1/4) ]
==> Tensor "object": 3 x 3 = 9 (C; dft = 0)
# File rtensor_sup.rb, line 497
497: def inverse ( ) # => new_tsr or nil
498: return nil if singular?
499: adj / det.to_f
500: end
Returns self after performing element-by-element left bitshift operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is left bitshift by t. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8].lbs!(Tsr[2, 3, 1]) => [ 16 56 16 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8].lbs!(2) => [ 16 28 32 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element right bitwise, see Tensor#rbs!.
# File rtensor_api.rb, line 1442
1442: def lbs! ( t ) # => self
1443: end
Returns a new tensor by performing element-by-element log (natural logarithm) operation.
Tsr[0, 1, 10].log => [ -Infinity 0.0 2.302585092994046 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1511
1511: def log ( ) # => new_tensor
1512: end
Returns a new tensor by performing element-by-element log10 (base-10 logarithm) operation.
Tsr[0, 1, 10].log10 => [ -Infinity 0.0 1.0 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1519
1519: def log10 ( ) # => new_tensor
1520: end
Returns self after performing element-by-element modulo operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is modulo divided by t. This operation is undefined for Float and Complex types.
Tsr[[7, 6], [3, 9]].mod!(Tsr[[2, 9], [3, 1]])
=>
[ 1 6
0 0 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[7, 6], [3, 9]].mod!(2.0)
=>
[ 1.0 0.0
1.0 1.0 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
For element-by-element divison, see Tensor#div!.
# File rtensor_api.rb, line 1390
1390: def mod! ( t ) # => self
1391: end
Returns self after performing element-by-element bitwise OR operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is bitwise OR by t. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8].or!(Tsr[5, 3, 4]) => [ 5 7 12 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8].or!(5) => [ 5 7 13 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element bitwise exclusive OR, see Tensor#xor!.
# File rtensor_api.rb, line 1416
1416: def or! ( t ) # => self
1417: end
Run the gnuplot plot command on the columns. Most of args are hashes for gnuplot. Returns the shape of the tensor.
t = Tsr.t([101, 3], :-, 0) do |i, m|
if m[1] == Tsr::Begin
(m[0] - Tsr::Begin) / 10.0
elsif m[1] == Tsr::Begin + 1
Math.sin((m[0] - Tsr::Begin) / 10.0)
elsif m[1] == Tsr::Begin + 2
Math.cos((m[0] - Tsr::Begin) / 10.0)
end
end
t.plot({:t1=>'Sin', :t2=>'Cos'}, {:w1=>'lp', :w2=>'lp'})
<<<plot "rtensor.txt" t "Sin" w lp, "" u 1:3 t "Cos" w lp>>>
=> [101, 3]
# File rtensor_sup.rb, line 519
519: def plot ( *args ) # => array
520: ensure_2D
521: usingFile = true
522:
523: if usingFile
524: fn = 'rtensor.txt'
525: file = File.new(fn, 'w')
526: if vector? && complex?
527:
528: else
529: (0...row_size).each {|i| file.puts(row(Begin + i).real.to_a.join(' '))}
530: end
531: file.close
532: else
533: fn ='-'
534: end
535:
536: title = []
537: with = []
538: (0...args.size).each do |i|
539: arg = args[i]
540: keys = arg.keys
541: (0...keys.size).each do |j|
542: key = keys[j]
543: key =~ /\d+/
544: num = $&
545: if key[0] =~ /t/
546: title[num.to_i] = arg[key]
547: elsif key[0] =~ /w/
548: with[num.to_i] = arg[key]
549: end
550: end
551: end
552:
553: cmd = 'plot "' + fn + '"'
554: cmd += ' u 1:2' if col_size > 1
555: cmd += ' t "' + title[1] + '"' if title[1]
556: cmd += ' w ' + with[1] if with[1]
557: (3..col_size).each do |j|
558: if fn == '-'
559: cmd += ', "' + fn + '"'
560: else
561: cmd += ', ""'
562: end
563: if usingFile
564: cmd += ' u 1:' + j.to_s
565: else
566: cmd += ' u 1:2'
567: end
568: cmd += ' t "' + title[j - 1] + '"' if title[j - 1]
569: cmd += ' w ' + with[j - 1] if with[j - 1]
570: end
571: puts '<<<' + cmd + '>>>'
572:
573: if usingFile
574: Tsr.gnuplot(cmd)
575: return shape
576: else
577: gp = IO.popen('gnuplot/binary/pgnuplot.exe -persist', 'w')
578: gp.puts(cmd)
579: if col_size == 1
580: (0...row_size).each {|i| gp.puts(self[Begin + i])}
581: gp.puts('e')
582: else
583: (1...col_size).each do |j|
584: (0...row_size).each {|i| gp.puts(self[Begin + i, Begin].to_s + ' ' + self[Begin + i, Begin + j].to_s)}
585: gp.puts('e')
586: end
587: end
588: gp.close_write
589: end
590: end
Returns a new tensor by performing element-by-element exponent operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is exponent by t. The default value and sparseness follow the tensor on the left of the pow sign.
Tsr[2, 3, 4].pow(Tsr[4, 2, 3]) => [ 16 9 64 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[2, 3, 4].pow(3) => [ 8 27 64 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For matrix exponentiation, see Tensor #**.
# File rtensor_api.rb, line 1333
1333: def pow ( t ) # => new_tensor
1334: end
Returns self after performing element-by-element right bitshift operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is right bitshift by t. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8].rbs!(Tsr[2, 3, 1]) => [ 1 0 4 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8].rbs!(2) => [ 1 1 2 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element left bitwise, see Tensor#lbs!.
# File rtensor_api.rb, line 1455
1455: def rbs! ( t ) # => self
1456: end
Returns a new tensor by performing element-by-element sin operation.
Tsr[0, 1, 2].sin => [ 0.0 0.8414709848078965 0.9092974268256817 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1527
1527: def sin ( ) # => new_tensor
1528: end
Returns true if the square 2D-tensor (matrix) is singular (i.e., its determinant is zero).
Tsr[[1.5, 7.5], [2, 10]].singular? => true
# File rtensor_sup.rb, line 597
597: def singular? ( ) # => true or false
598: det == 0
599: end
Returns a new tensor by performing element-by-element sinh operation.
Tsr[0, 1, 2].sinh => [ 0.0 1.1752011936438014 3.6268604078470186 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1535
1535: def sinh ( ) # => new_tensor
1536: end
Returns a new tensor by performing element-by-element sqrt operation.
Tsr[1, 2, 4].sqrt => [ 1.0 1.4142135623730951 2.0 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1543
1543: def sqrt ( ) # => new_tensor
1544: end
Returns self after performing element-by-element subtraction. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is subtracted by t.
Tsr[[1, 5], [4, 2]].sub!(Tsr[[9, 3], [-4, 1]])
=>
[ -8 2
8 1 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
Tsr[[1, 5], [4, 2]].sub!(3)
=>
[ -2 2
1 -1 ]
==> Tensor "object": 2 x 2 = 4 (C; dft = 0)
For element-by-element addition, see Tnsr#add!.
# File rtensor_api.rb, line 1360
1360: def sub! ( t ) # => self
1361: end
Returns a new tensor by performing element-by-element tan operation.
Tsr[0, 1, 2].tan => [ 0.0 1.5574077246549023 -2.185039863261519 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1551
1551: def tan ( ) # => new_tensor
1552: end
Returns a new tensor by performing element-by-element tanh operation.
Tsr[0, 1, 2].tanh => [ 0.0 0.7615941559557649 0.9640275800758169 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
# File rtensor_api.rb, line 1559
1559: def tanh ( ) # => new_tensor
1560: end
Returns an GSL matrix representation of the 2D-tensor.
t = Tsr.t([1, 2, 3], [2, 4])
=>
[ 1 2 3 0
0 0 0 0 ]
==> Tensor "double": 2 x 4 = 8 (C; dft = 0)
n = t.to_gsl
=> GSL::Matrix
[ 1.000e+000 2.000e+000 3.000e+000 0.000e+000
0.000e+000 0.000e+000 0.000e+000 0.000e+000 ]
# File rtensor_sup.rb, line 613
613: def to_gsl ( mtrx = true ) # => GSL::Matrix
614: ensure_2D
615: require 'gsl'
616: if complex?
617: unless mtrx
618: g = GSL::Vector::Complex(real.to_a, imag.to_a)
619: else
620: g = GSL::Matrix::Complex.alloc(row_size, col_size)
621: if row_order?
622: t = self
623: else
624: unless $RowOrder
625: $RowOrder = true
626: t = self[]
627: $RowOrder = false
628: end
629: end
630: (0...length).each {|k| g.set(k, GSL::Complex[t[Begin + k].real, t[Begin + k].imag])}
631: end
632: else
633: unless mtrx
634: if num_limits =~ /is_integer = 1/
635: g = GSL::Vector::Int[self.to_a]
636: else
637: g = GSL::Vector[self.to_a]
638: end
639: else
640: ary = []
641: (0...row_size).each {|i| ary.push(row(Begin + i).to_a)}
642: if num_limits =~ /is_integer = 1/
643: g = GSL::Matrix::Int[*ary]
644: else
645: g = GSL::Matrix[*ary]
646: end
647: end
648: end
649: g
650: end
Returns an NArray representation.
t = Tsr.t([1, 2, 3], [2, 4])
=>
[ 1 2 3 0
0 0 0 0 ]
==> Tensor "double": 2 x 4 = 8 (C; dft = 0)
n = t.to_narray
=> NArray.float(4,2):
[ [ 1.0, 2.0, 3.0, 0.0 ],
[ 0.0, 0.0, 0.0, 0.0 ] ]
# File rtensor_sup.rb, line 664
664: def to_narray ( ) # => NArray
665: require 'narray'
666: case ttype
667: when 'uchar'
668: typecode = 'byte'
669: when 'short'
670: typecode = 'sint'
671: when 'int'
672: typecode = 'int'
673: when 'float'
674: typecode = 'sfloat'
675: when 'double'
676: typecode = 'float'
677: when 'cfloat'
678: typecode = 'scomplex'
679: when 'cdouble'
680: typecode = 'complex'
681: else
682: typecode = 'object'
683: end
684: size = dim.to_a
685: size[0] = dim[Begin + 1]
686: size[1] = dim[Begin]
687:
688: na = NArray.new(typecode, *size)
689:
690: if row_order?
691: t = self
692: else
693: unless $RowOrder
694: $RowOrder = true
695: t = self[]
696: $RowOrder = false
697: end
698: end
699: (0...length).each {|k| na[k] = t[Begin + k]}
700: na
701: end
Returns a string representation.
# File rtensor_api.rb, line 1576
1576: def to_s ( ) # => str
1577: end
Returns self after performing element-by-element bitwise exclusive OR operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is bitwise exclusive OR by t. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8].xor!(Tsr[5, 3, 4]) => [ 1 4 12 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8].xor!(5) => [ 1 2 13 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element bitwise OR, see Tensor#or!.
# File rtensor_api.rb, line 1429
1429: def xor! ( t ) # => self
1430: end
Returns a new tensor by performing element-by-element bitwise OR operation. If t is a tensor, the two tensors must have equal number of total elements and currently they have to be of exactly the same type; else each element is bitwise OR by t. The default value and sparseness follow the tensor on the left of the | sign. This operation is undefined for Float and Complex types.
Tsr[4, 7, 8] | Tsr[5, 3, 4] => [ 5 7 12 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0) Tsr[4, 7, 8] | 5 => [ 5 7 13 ] ==> Tensor "object": 1 x 3 = 3 (C; dft = 0)
For element-by-element bitwise exclusive OR, see Tensor #^.
# File rtensor_api.rb, line 1209
1209: def | ( t ) # => new_tensor
1210: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.