How to replace the the diagonal elements of a matrix by a vector in SymPy?
I have a vector X which I created like this: from sympy import * x1 = Symbol('x1') x2 = Symbol('x2') x3 = Symbol('x3') X = Matrix([x1, x2, x3]) Then I also have a matrix myMat which just contains ones: myMat = ones(3, 3) Matrix([ [1, 1, 1], [1, 1, 1], [1, 1, 1]]) Now I would like to replace the diagonal of the matrix by my vector X ; my desired outcome looks like this: Matrix([ [x1, 1, 1], [1, x2, 1], [1, 1, x3]]) I can of course do it in a for-loop like this: for ind, el in enumerate(X): myMat[ind, ind] = el but I am wondering whether there is a smarter way of doing that by directly accessing