all files / algebra/src/ TensorSpace.js

94.74% Statements 72/76
83.33% Branches 5/6
84.62% Functions 11/13
94.59% Lines 70/74
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                   40×           40×               69×             40×   69×   40×           38× 67× 147×     67×               375×         99×   99× 99×   99×     76× 43×   43× 138×   138× 318×     138×     43×       38×   38× 76×   76× 21× 21×   21×   21×   21×           16×         38×   38×       38×   22×           38×     38×               38×         38×   38× 76× 76× 76×       38×        
var operators = require('./operators.json')
var staticProps = require('static-props')
var toData = require('./toData')
var tensorProduct = require('tensor-product')
 
/**
 * Creates a tensor space that is a class representing a tensor.
 *
 * @param {Object} Scalar
 *
 * @returns {Function} anonymous with signature (indices)
 */
 
function TensorSpace (Scalar) {
  var multiplication = Scalar.multiplication
 
  /**
   * @param {Array} indices
   */
 
  return function (indices) {
    // If dim equals 1 it is like a vector of dimension 1, that is a scalar.
    // Only dim greater than 1, represents a varying index  increase order.
    // A scalar has order 0.
    // A vector has order 1.
    // A matrix has order 2.
    // Order is also called "rank" or "tensor rank", but, to avoid confusion with
    // "matrix rank" it is better to call it "order".
    var order = indices.filter((dim) => dim > 1).length
 
    // TODO if it is a scalar, return the Scalar
    // which should be a composition algebra
    // Then add product tensor to composition algebras.
    // Finally, a tensor i,j,k should be constructed as the
    // tensor product of a scalar i,j,k times.
    var isScalar = (order === 0)
 
    var dimension = indices.reduce((a, b) => a * b, 1)
 
    if (isScalar) {
      staticProps(Scalar)({ order })
 
      return Scalar
    }
 
    // TODO create one for square matrices
    // Create zero.
    var zero = indices.reduce((result, dim) => {
      for (var i = 0; i < dim; i++) {
        result.push(Scalar.zero)
      }
 
      return result
    }, [])
 
    /**
     */
 
    function Tensor (data) {
      // validate data
 
      function validate (item) {
        Iif (Scalar.notContains(item)) {
          throw new TypeError('Invalid data = ' + item)
        }
      }
 
      data.forEach(validate)
 
      var enumerable = true
      staticProps(this)({ data }, enumerable)
 
      staticProps(this)({ order })
    }
 
    function staticBinary (operator) {
      Tensor[operator] = function () {
        var result = []
 
        for (var i = 0; i < dimension; i++) {
          var operands = []
 
          for (var j = 0; j < arguments.length; j++) {
            operands.push(toData(arguments[j])[i])
          }
 
          result.push(Scalar[operator].apply(null, operands))
        }
 
        return result
      }
    }
 
    var myBinaryOperators = ['addition', 'subtraction']
 
    myBinaryOperators.forEach((operator) => {
      staticBinary(operator)
 
      Tensor.prototype[operator] = function () {
        var args = [].slice.call(arguments)
        var operands = [this.data].concat(args)
 
        var data = Tensor[operator].apply(null, operands)
 
        var tensor = new Tensor(data)
 
        return tensor
      }
    })
 
    function scalarMultiplication (tensor, scalar) {
      var tensorData = toData(tensor)
 
      var result = []
 
      for (var i = 0; i < dimension; i++) {
        result.push(multiplication(tensorData[i], scalar))
      }
 
      return result
    }
 
    Tensor.scalarMultiplication = scalarMultiplication
 
    Tensor.prototype.scalarMultiplication = function (scalar) {
      var data = scalarMultiplication(this, scalar)
 
      return new Tensor(data)
    }
 
    Tensor.equality = function (tensor1, tensor2) {
      var tensorData1 = toData(tensor1)
      var tensorData2 = toData(tensor2)
 
      for (var i = 0; i < dimension; i++) {
        if (Scalar.disequality(tensorData1[i], tensorData2[i])) {
          return false
        }
      }
 
      return true
    }
 
    Tensor.prototype.equality = function (tensor2) {
      return Tensor.equality(this, tensor2)
    }
 
    Tensor.product = function (leftData) {
      return (rightDim) => {
        return function (rightData) {
          return tensorProduct(multiplication, indices, rightDim, leftData, rightData)
        }
      }
    }
 
    staticProps(Tensor)({
      order,
      zero
    })
 
    var myOperators = operators.group
 
    myOperators.forEach((operator) => {
      operators.aliasesOf[operator].forEach((alias) => {
        Tensor[alias] = Tensor[operator]
        Tensor.prototype[alias] = Tensor.prototype[operator]
      })
    })
 
    return Tensor
  }
}
 
module.exports = TensorSpace