Skip to content

Creation

Uri edited this page Aug 5, 2021 · 6 revisions

Vectors

Vector is a one-dimensional array.

A Vector can be declared:

  • using native lisp syntax, and it's values can be any applicable number or boolean value
> [1 2 3 4 5]
#(1 2 3 4 5)

> [0 -3 3/7 16+4i 7.12 (inexact 7.12) 123456789876543213546576666777757575757444 +inf.0]
#(0 -3 3/7 16+4i 178/25 7.12 123456789876543213546576666777757575757444 +inf.0)
  • as a copy of existing vector
> (define v [1 2 3 4 5])
> (print v)
#(1 2 3 4 5)
> (Copy v)
#(1 2 3 4 5)
  • as an uninializied vector of N elements (it's not guaranteed that the vector will be initialized with zeros, it can be a #false constants for example)
> (Vector 14)
#(0 0 0 0 0 0 0 0 0 0 0 0 0 0)
  • as a vector of zeros
> (Zeros 14)
#(0 0 0 0 0 0 0 0 0 0 0 0 0 0)
  • as a vector of 1s
> (Ones 42)
#(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
  • as a vector of any applicable repeating value
> (Fill (Vector 17) -33)
#(-33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33)
  • as a vector of a dynamically computed values
> (Fill (Vector 17)
     (lambda ()
        (rand! 123)))
#(110 8 119 79 8 75 111 93 87 15 63 122 87 46 110 74 94)
> (Fill (Vector 17)
     (lambda ()
        (inexact (/ (rand! 100000) 1000))))
#(30.9909999 31.26 0.384 4.85299999 6.53 18.5719999 15.7949999 18.1829999 16.8689999 11.797 8.814 28.001 30.69 10.473 32.006 5.157 30.3659999)

Matrices

Matrix is a two-dimensional array.

A Matrix can be declared:

  • using native lisp syntax, and it's values can be any applicable number or boolean value
> [[1 2 3]
   [4 5 6]
   [7 8 9]]
#(#(1 2 3) #(4 5 6) #(7 8 9))
  • as a copy of existing matrix
> (define m [[1 2 3] [4 5 6] [7 8 9]])
> (print m)
#(#(1 2 3) #(4 5 6) #(7 8 9))
> (Copy m)
#(#(1 2 3) #(4 5 6) #(7 8 9))
  • as a matrix of N identical vectors
> (Matrix [1 2 3] 4)
#(#(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3))
  • as an uninializied matrix of N(rows) * M(columns) elements (it's not guaranteed that the matrix will be initialized with zeros, it can be a #false constants for example)
> (Matrix 3 4)
#(#(0 0 0 0) #(0 0 0 0) #(0 0 0 0))
  • as a matrix of zeros, 1's
> (Zeros 3 7)
#(#(0 0 0 0 0 0 0) #(0 0 0 0 0 0 0) #(0 0 0 0 0 0 0))

> (Ones 1 3)
#(#(1 1 1))
  • as a matrix of zeros (ones) with the same shape of another matrix
> (define m (Matrix 2 5))
> (print m)
#(#(0 0 0 0 0) #(0 0 0 0 0))
> (Ones m)
#(#(1 1 1 1 1) #(1 1 1 1 1))
  • as a matrix of any applicable repeating value or a dynamically computed values
> (Fill (Matrix 2 3) -1)
#(#(-1 -1 -1) #(-1 -1 -1))
> (Fill (Matrix 2 3)
     (lambda ()
        (rand! 100)))
#(#(46 15 63) #(70 1 69))

Tensors

Tensor is a three- and over-dimensional array with additional applicable math rules.

A Tensor can be declared:

  • using native lisp syntax, and it's values can be any applicable number or boolean value
> (define t [[[1 2 3] [4 5 6] [7 8 9]]
             [[2 2 2] [3 3 3] [4 4 4]]])
#(#(#(1 2 3) #(4 5 6) #(7 8 9)) #(#(2 2 2) #(3 3 3) #(4 4 4)))
> (print (Shape t))
(2 3 3)
  • as an uninializied tensor of N1N2...*Nn elements (it's not guaranteed that the tensor will be initialized with zeros, it can be a #false constants for example)
> (Tensor 2 3 2 2)
#(#(#(#(0 0) #(0 0)) #(#(0 0) #(0 0)) #(#(0 0) #(0 0))) #(#(#(0 0) #(0 0)) #(#(0 0) #(0 0)) #(#(0 0) #(0 0))))
  • as a tensor of zeros, 1's
> (Zeros (Tensor 2 3 2 2))
> (Ones (Tensor 2 3 2 2))
#(#(#(#(0 0) #(0 0)) #(#(0 0) #(0 0)) #(#(0 0) #(0 0))) #(#(#(0 0) #(0 0)) #(#(0 0) #(0 0)) #(#(0 0) #(0 0))))
#(#(#(#(1 1) #(1 1)) #(#(1 1) #(1 1)) #(#(1 1) #(1 1))) #(#(#(1 1) #(1 1)) #(#(1 1) #(1 1)) #(#(1 1) #(1 1))))
  • as a tensor of any applicable repeating value or a dynamically computed values
> (Fill (Tensor 2 3 2) -1)
#(#(#(-1 -1) #(-1 -1) #(-1 -1)) #(#(-1 -1) #(-1 -1) #(-1 -1)))
> (Fill (Tensor 2 3 2)
     (lambda ()
        (rand! 2)))
#(#(#(1 1) #(1 0) #(1 1)) #(#(0 1) #(1 0) #(1 1)))
Clone this wiki locally