+/** Product of matrix and scalar expression. */
+matrix matrix::mul_scalar(const ex & other) const
+{
+ if (other.return_type() != return_types::commutative)
+ throw std::runtime_error("matrix::mul_scalar(): non-commutative scalar");
+
+ exvector prod(row * col);
+
+ for (unsigned r=0; r<row; ++r)
+ for (unsigned c=0; c<col; ++c)
+ prod[r*col+c] = m[r*col+c] * other;
+
+ return matrix(row, col, prod);
+}
+
+
+/** Power of a matrix. Currently handles integer exponents only. */
+matrix matrix::pow(const ex & expn) const
+{
+ if (col!=row)
+ throw (std::logic_error("matrix::pow(): matrix not square"));
+
+ if (is_exactly_a<numeric>(expn)) {
+ // Integer cases are computed by successive multiplication, using the
+ // obvious shortcut of storing temporaries, like A^4 == (A*A)*(A*A).
+ if (expn.info(info_flags::integer)) {
+ numeric b = ex_to<numeric>(expn);
+ matrix A(row,col);
+ if (expn.info(info_flags::negative)) {
+ b *= -1;
+ A = this->inverse();
+ } else {
+ A = *this;
+ }
+ matrix C(row,col);
+ for (unsigned r=0; r<row; ++r)
+ C(r,r) = _ex1;
+ if (b.is_zero())
+ return C;
+ // This loop computes the representation of b in base 2 from right
+ // to left and multiplies the factors whenever needed. Note
+ // that this is not entirely optimal but close to optimal and
+ // "better" algorithms are much harder to implement. (See Knuth,
+ // TAoCP2, section "Evaluation of Powers" for a good discussion.)
+ while (b!=*_num1_p) {
+ if (b.is_odd()) {
+ C = C.mul(A);
+ --b;
+ }
+ b /= *_num2_p; // still integer.
+ A = A.mul(A);
+ }
+ return A.mul(C);
+ }
+ }
+ throw (std::runtime_error("matrix::pow(): don't know how to handle exponent"));
+}
+
+
+/** operator() to access elements for reading.