Method: AppMath::Mat#*

Defined in:
lib/linalg.rb

#*(v) ⇒ Object

Multiplication of a Mat with either a Mat, Vec, or Numeric



926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'lib/linalg.rb', line 926

def *(v)
  d1 = dim1
  fail "dim1 == 0" if d1.zero?
  d2 = dim2
  fail "dim2 == 0" if d2.zero?
  zero=@x[0].x[0].to_0
  if v.is_a?(Mat)
    d3 = v.dim1
    fail "dimenson mismatch" unless d3 == d2
    d4 = v.dim2
    fail "dim4 == 0" if d4.zero?
  # we better produce the d1 row-vectors in turn

    a = Array.new(d1)
    for i in 0...d1
      vi = Vec.new(d4,zero)
      for j in 0...d4
        vij = zero
        for k in 0...d2
          vij += @x[i].x[k] * v.x[k].x[j]
        end
        vi.x[j] = vij
      end
      a[i] = vi
    end
    res = Mat.new(a)
  elsif v.is_a?(Vec) 
    d3 = v.dim
    fail "dimenson mismatch" unless d3 == d2
    res = Vec.new(d1,zero)
    for i in 0...d1
      vi = zero
      for j in 0...d2
        vi += @x[i].x[j] * v.x[j]
      end
      res.x[i] = vi
    end
  elsif v.is_a?(Numeric) # multiplication with scalar

    res = clone
    for i in 1..d1
      res[i] *= v
    end
  else
    fail "can't multiply with this object"
  end
  res
end