Hot answers tagged matrix
25
With some diffidence (because there appears to be a Mathematica bug: see below), I would like to offer an answer in the spirit of the OP's original attempt to solve the problem algebraically.
Solution
This problem can be formulated as a binary integer linear program. The reformulation represents the square (or more generally, a rectangle as implemented ...
24
In this article the author solves the problem of tiling a rectangle by using pieces taken from a set of polyominoes, which are plane geometric figures formed by joining one or more equal squares edge to edge. For example, these are the pentaminoes, polyominoes formed by joining 5 squares:
Of course this problem is more difficult than the one you asked ...
22
==== Method 1 ===
Here is a way to get a graph from an image. MorphologicalGraph can get you started.
img = Import["http://i.stack.imgur.com/9HXZ5.png"];
g = MorphologicalGraph[img]
And here is your KirchhoffMatrix of the graph. Please note that MatrixPlot averages values for the best visual representation, - actual plot would be too detailed to be a ...
17
It's just a matter of the difficulty inherent in the numerical computation of determinants. Here's what Cleve Moler has to say about determinants and characteristic polynomials in chapter 10 of his book on numerical computing:
Like the determinant itself, the characteristic polynomial is useful in theoretical considerations and hand calculations, but ...
16
Mathematica offers a pretty complete set of functionality for linear algebra, and it has improved in recent versions.
For example, since version 5, Mathematica has offered the generalised Schur decomposition (also known as the QZ decomposition). This certainly wasn't available in earlier versions. It handles sparse matrices and many other wrinkles. And if ...
16
Try something like
matrix = SparseArray[{{i_, i_} /; OddQ[i] -> -3, {i_, i_} /; EvenQ[i] ->
2}, {2012,2012}, 1] // Normal;
If you are interested in the formal solution for it determinant,
Clear[matrix];
matrix[n_] := SparseArray[{{i_, i_} /; OddQ[i] -> -3, {i_, i_} /; EvenQ[i] ->
2}, {n, n}, 1];
you can start guessing a recursion from
...
16
Time-dependent case
in the time-dependent case, $[H(t),H(t')]\neq0$ in general and we need to time-order, ie, the operator taking a state from $t=0$ to $t=\tau$ is $U(0,\tau)=\mathcal{T}\exp(-i\int_0^\tau dt\, H(t))$ with $\mathcal{T}$ the time-ordering operator. In practice we just split the time interval into lots of small pieces (basically using the ...
14
a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
b = {{1, 2}, {3, 4}};
ArrayFlatten[{{a, 0}, {0, b}}] // MatrixForm
You can Fold this operation over a list of matrices to get a diagonal:
a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
b = {{1, 2}, {3, 4}};
c = {{1, 2, 3}, {4, 5, 6}};
d = {{1, 2}, {3, 4}, {5, 6}};
Fold[ArrayFlatten[{{#, 0}, {0, #2}}] &, a, {b, c, ...
14
The rank of a matrix is typically determined by performing a Gaussian elimination and is given by the number of non-zero rows. In your second case, the large number $5.4\times 10^{12}$, when eventually used as a pivot, gives a badly conditioned matrix (myM2 is the second matrix in your question):
RowReduce[myM2]
RowReduce::luc: Result for RowReduce of ...
13
Here is a very simple way to do it:
Table[1/i! D[M, {a, i}] /. a -> 0, {i, 0, 3}]
(*
==> {{{15, 0}, {0, 2}}, {{0, 1}, {1, 0}}, {{1, 5}, {-5, 0}}, {{0, 0}, {0, 0}}}
*)
This works even if the entries are not polynomials. If they are, you can replace the arbitrary maximum 3 in the Table index by the degree of the polynomial:
Max[Exponent[M, a]]
...
13
lists = RandomInteger[{1, 9}, {4, 5}]
{{7, 4, 9, 9, 7},
{4, 2, 5, 5, 2},
{6, 5, 9, 2, 4},
{1, 9, 4, 7, 2}}
ArrayPad[lists, {{0, 0}, {1, 0}}]
{{0, 7, 4, 9, 9, 7},
{0, 4, 2, 5, 5, 2},
{0, 6, 5, 9, 2, 4},
{0, 1, 9, 4, 7, 2}}
There are of course many ways to do this.
Since you are interested in learning here are some others, more or less ...
13
array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}};
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] ->
array0[[#]] & /@ {1, 2, 3}, {5, 5}];
array1 // MatrixForm
Update: Generalizing for arbitrary matrix input:
rttF = Function[{mat}, With[{dims = Dimensions[mat]},
SparseArray[Band[{# - 1 + Last@dims, #}, ...
11
Here is simple (unweighted) Mma version of the Matlab implementation
of Covariance Bending.
ClearAll[covBending];
covBending[mat_, tol_: 1/10000]:=If[PositiveDefiniteMatrixQ[mat], mat,
NestWhile[(Eigensystem[#][[2]].DiagonalMatrix[
Max[#, tol] & /@(Eigensystem[#][[1]])].Transpose[
Eigensystem[#][[2]]]) &, N@mat, Min[Eigensystem[#][[1]]] < ...
11
n = 8; m = 4;
origin ={6,7};
matrix = Table[0, {n}, {n}];
sub2 = Array[s, {m, m}];
sub3 = RandomReal[{.2, 1}, {m, m}];
sub3[[1,1]]=2;
{dimmat, dimsub} = Dimensions /@ {matrix, sub2};
(* using Istvan's suggestion for generating row and column indices of the part to be replaced:*)
{rows, columns} = Mod[Range @@@ Transpose@{origin, origin + dimsub - ...
11
Use
Eigenvalues[mat, Cubics -> True]
Eigenvectors[mat, Cubics -> True]
sometimes Quartics -> Truecan be needed.
or
ToRadicals @ Eigenvalues[ mat]
ToRadicals @ Eigenvectors[ mat]
In general one cannot find roots (of higher order) polynomials in terms of radicals. The reason that Mathematica allows this option is that in general it is ...
11
b.gatessucks's answer shows you what to do and is worthy of the accept. However, it does not explain why your previous approach wasn't going to work.
First, a point on efficiency. In your For loop, you were redefining iM and staDis each iteration of the loop, even though the values didn't change. This is unnecessary. Similarly, you didn't really need both ...
10
There is no need for the Modulus option in CharacteristicPolynomial, since PolynomialMod serves that purpose. Assume we have a matrix m e.g. :
m = RandomInteger[10, {5, 5}]
m // MatrixForm
{{10, 1, 4, 10, 9}, {1, 9, 6, 1, 5}, {9, 7, 9, 1, 0}, {1, 10, 8, 0, 4}, {4, 0, 4, 7, 10}}
then
CharacteristicPolynomial[m, x]
2310 - 4008 x + 1739 x^2 - ...
10
You can create your new diagonal matrix V in a single step as:
V = DiagonalMatrix@SparseArray[1/Normal[Diagonal[A]]];
On my machine, this takes 0.05 seconds, compared to 9 seconds for your code above (excluding time taken to construct A).
You can verify that they're both the same:
DiagonalMatrix[SparseArray[B]] == ...
10
Please see Graphs and Matrices guide page for related functionality. Below is usage summary.
This is creates a random graph with 9 vertexes and edge 0.4 probability. SeedRandom allows to reproduce the graph across multiple evaluations.
SeedRandom[3]; g = RandomGraph[BernoulliGraphDistribution[9, 0.4],
VertexLabels -> Placed["Name", Center], ...
10
In years past I've taught a standard-content sophomore-level (in U.S.) linear algebra course where students used Mathematica. I know, then, that if you're interested, or if it's a requirement, you can build everything up from the simplest functions for manipulating matrices or you can directly use powerful built-in Mathematica functions (or a combination of ...
10
If you have a list of matrices, you can apply a dot multiplication to all the matrices by changing the Head of the list to Dot. To illustrate this, I'll first define a set of matrices symbolically:
matrixList = With[{numberOfMatrices = 2},
Table[Array[Subsuperscript["M", Row[{#1, #2}], i] &, {2, 2}], {i,
numberOfMatrices}]
]
$\left\{\left(
...
10
Let me show you a way which hopefully helps you to solve such things alone next time. If you cannot implement your overall task, have you asked yourself, whether you can solve a sub-problem of your task? For instance, if you have one row of a and one row of b would you be able to combine this into the resulting row?
Basically, you are only interested in the ...
10
diagF = With[{dims = Total@(Dimensions /@ {##})},
SparseArray[Band[{1, 1}, dims] -> {##}, dims]] &;
Edit: Much more elegant form (thanks to Mr.Wizard)
diagF = SparseArray[Band[{1, 1}] -> {##}] &
Example:
a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
b = {{1, 2}, {3, 4}};
c = {{1, 2, 3}, {4, 5, 6}};
d = {{1, 2}, {3, 4}, {5, 6}};
...
10
The question is, what do you want to do with the output. The output of Position is in a form so that it can directly be used with Extract
list = {a, b, a, a, b, c, b};
pos = Position[list, b];
Extract[list, pos]
(* {b, b, b} *)
For this simple example, it is a bit useless because we already know, that on all positions pos we have a b in list. ...
9
Here's one quick way for polynomial matrices:
polyMat = {{15 + a^2, a + 5 a^2}, {a - 5 a^2, 2}};
Transpose[PadRight[CoefficientList[polyMat, a]], {2, 3, 1}]
{{{15, 0}, {0, 2}}, {{0, 1}, {1, 0}}, {{1, 5}, {-5, 0}}}
Alternatively (as Jens hints), you can do Flatten[PadRight[CoefficientList[polyMat, a]], {3}].
You can check that the matrix polynomial is ...
9
Proposed solution
If fn[i,j] produces the $(i,j)^{th}$ element, then
makeSym[size_, fn_] := Module[
{rtmp},
rtmp = Table[
fn[i, j],
{i, 1, size},
{j, 1, i}];
MapThread[
Join,
{rtmp, Rest /@ Flatten[rtmp, {{2}, {1}}]}
]
]
does what you want.
Example
makeSym[5, Subscript[f, #1, #2] &] // MatrixForm
How does it work?
...
9
list = {{1, 2}, {3, 4}, {5, 6}};
Flatten /@ Tuples[{{0}, list}]
{{0, 1, 2}, {0, 3, 4}, {0, 5, 6}}
Join @@@ Tuples[{{{0}}, list}]
the latter method (thanks to kguler) is even faster than the former one.
These toys haven't been mentioned yet :
Join[ {0}, #] & /@ list
Flatten /@ ({0, #} & /@ list)
MapThread[ Join, {ConstantArray[{0}, ...
9
There is no unique solution to your problem, but you can use Reduce to find all solutions.
Say your matrix is m:
m = {{0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, ...
9
Rob Knapp has some excellent publicly available notebooks for some of the things you might want. Here are three in particular that may be useful.
http://www.mathematica-journal.com/issue/v7i4/features/knapp/
http://library.wolfram.com/infocenter/Conferences/288/
http://library.wolfram.com/infocenter/Conferences/7968/
Among other things they give explicit ...
9
For matrices of numbers this is fairly efficient.
perm[mat_] := Module[{v, vec},
vec = Array[v, Length[mat]];
Coefficient[Times @@ (mat.vec), Times @@ vec]
]
permMinors[mat_, k_Integer] := Minors[mat, k, perm]
Example:
n = 12;
mat = RandomInteger[{-10, 10}, {n, n}];
Timing[p1 = permMinors[mat, n - 1];]
(* Out[228]= {19.910000, Null} *)
--- edit ...
Only top voted, non community-wiki answers of a minimum length are eligible

