878 
878 
end

879 
879 

880 
880 
#

881 

# Matrix addition.


881 
# Matrix addition and subtraction.

882 
882 
# Matrix.scalar(2,5) + Matrix[[1,0], [4,7]]

883 
883 
# => 6 0

884 
884 
# 4 12

885 
885 
#

886 

def +(m)

887 

case m

888 

when Numeric

889 

Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class

890 

when Vector

891 

m = self.class.column_vector(m)

892 

when Matrix

893 

else

894 

return apply_through_coercion(m, __method__)

895 

end

896 


897 

Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count and column_count == m.column_count

898 


899 

rows = Array.new(row_count) {i

900 

Array.new(column_count) {j

901 

self[i, j] + m[i, j]

902 

}

903 

}

904 

new_matrix rows, column_count

905 

end

906 


907 

#

908 

# Matrix subtraction.

909 
886 
# Matrix[[1,5], [4,2]]  Matrix[[9,3], [4,1]]

910 
887 
# => 8 2

911 
888 
# 8 1

912 
889 
#

913 

def (m)

914 

case m

915 

when Numeric

916 

Matrix.Raise ErrOperationNotDefined, "", self.class, m.class

917 

when Vector

918 

m = self.class.column_vector(m)

919 

when Matrix

920 

else

921 

return apply_through_coercion(m, __method__)

922 

end


890 
%i(+ ).each do op


891 
define_method(op) do m


892 
case m


893 
when Numeric


894 
Matrix.Raise ErrOperationNotDefined, op, self.class, m.class


895 
when Vector


896 
m = self.class.column_vector(m)


897 
when Matrix


898 
else


899 
return apply_through_coercion(m, op)


900 
end

923 
901 

924 

Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count and column_count == m.column_count


902 
Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count

925 
903 

926 

rows = Array.new(row_count) {i

927 

Array.new(column_count) {j

928 

self[i, j]  m[i, j]


904 
rows = Array.new(row_count) {i


905 
Array.new(column_count) {j


906 
self[i, j].send(op, m[i, j])


907 
}

929 
908 
}

930 

}

931 

new_matrix rows, column_count


909 
new_matrix rows, column_count


910 
end

932 
911 
end

933 
912 

934 
913 
#
