Hot answers tagged

10

wrap the matrix rows with the the Flatten function M = {{{1}, {2}, {3}, {4}}, {{5}, {6}, {7}, {8}}} To save time you can wrap your whole matrix using: Map[Flatten, <yourmatrix> ] Map[Flatten,{{{1}, {2}, {3}, {4}}, {{5}, {6}, {7}, {8}}}] the outermost list contains two elements (the rows). the Map function wraps these elements with the ...


10

tl;dr If these functions cannot decide, they will simply return False. A False result means that the selected equality testing method wasn't able to prove equality, but it does not mean that it was able to prove inequality. Interpret the result relative to the used SameTest option value. I will try to explain what I think is happening, though some of ...


9

Lots of solutions. Time for a benchmark. My own contribution is Part: m = {{{1}, {2}, {3}}, {{2}, {4}, {6}}}; m[[All, All, 1]]; {{1, 2, 3}, {2, 4, 6}} Update: I made a complete mess of my earlier attempt at benchmarking. Here is a rewrite. methods = Hold[Flatten /@ m, ArrayReshape[m, Most@Dimensions@m], ArrayReshape[m, Dimensions[m][[1 ;; ...


8

After some investigation I can mostly explain the (correct) behavior. First off, we set this up using machine doubles. After computing the eigensystems we reorder by eigenvalues. While there is the possibility of messing up conjugate pairs (wherein right and left eigenvalues get ordered differently due to small numeric discrepancies), I checked and this ...


7

List@*Total /@ W % === g $\ $ True


7

The following is ten times faster. The remaining time is mostly consumed while evaluating your function, so there may be some optimization window there. point1[j_] := Join[x[[;; j]], w[[j + 1 ;;]]]; point2[j_] := Join[x[[;; j - 1]], w[[j ;;]]]; max = 11; fx = f[x]; β = SparseArray[{{i_, i_} -> -1/100}, {size, size}]; Do[{ w = x + β.fx; T = ...


7

You can use ArrayReshape. Either ArrayReshape[mat, Most@Dimensions@mat] or ArrayReshape[mat, Dimensions[mat][[1 ;; 2]]] It will keep a packed array packed, too.


7

Here's a nice one-liner: TransformationFunction[{{1, 0, 0, 0}, {0, 0, -1, 0}, {0, 1, 0, 3}, {0, 0, 0, 1}}] // InverseFunction TransformationFunction[{{1, 0, 0, 0}, {0, 0, 1, -3}, {0, -1, 0, 0}, {0, 0, 0, 1}}] Note that TransformationFunction[] is the head of the results returned by geometric *Transform functions, which take a homogeneous transformation ...


6

MapThread[ Append, {Outer[Join, a, a, 1], d}, 2 ]


6

One way: m2 = Apply[{#3, #2, #, #4} &, m1, {2}] or another: m2 = m1; m2[[;; , ;; , {3, 1}]] = m2[[;; , ;; , {1, 3}]]; m2


6

Edit With m = RandomInteger[9, {3, 6, 1}] Just for completeness: Catenate /@ m Just for diversity reasons Apply[#&, m, {2}] Apply[Sequence, m, {-2}] Map[First, m, {2}] Using @Mr.Wizard code I have updated the benchmark. @MichaelE2 also noticed in comments that Catenate is not compilable.


5

List@*Total /@ W (V10 only) List /@ Total /@ W (V10 or earlier) (* {{Subscript[x, 11] + Subscript[x, 12] + Subscript[x, 13]}, {Subscript[x, 21] + Subscript[x, 22] + Subscript[x, 23]}, {Subscript[x, 31] + Subscript[x, 32] + Subscript[x, 33]}} *) other alternatives: List /@ Plus @@@ W List /@ Total[W, {2}]


5

tf[nn_] := With[{n = Range[0, nn]}, ToeplitzMatrix[(-1)^n n x, -(-1)^n n x]] For the question in the comment: tf2[nn_] := With[{n = Range[0, nn]}, Module[{expr = (-1)^n Cot@(n x)}, expr[[1]] = 0; ToeplitzMatrix[expr, -expr]]] tf3[nn_] := With[{n = Range@nn}, With[{expr = Join[{0}, (-1)^n Cot@(n x)]}, ToeplitzMatrix[expr, -expr]]]


5

m3 = m1 /. ({#, #2, #3, _} -> {##} & @@@ Flatten[m2, 1]) { {{1, 2, 2, 3}, {1, 1, -2, 1}, {3, 2, 2, -I}, {1, 2, 0, -I}}, {{3, 4, 4, 0}, {1, 1, 4, 1}, {3, 3, 4, -1}, {1, 1, 2, I}} } So basically we create replacement rules from m2, e.g. from {1,0,2,1} we get {1,0,2,_} -> {1,0,2,1}. Then we replace it in m1, if {a,b,c,_} matches, those 3 ...


5

Let's build your matrix in three steps. a = {{1, 2, 2, 1}, {3, 4, 4, 3}, {8, 5, 5, 8}}; d = {{I, 2, -I}, {I, 1, -1}, {4, I, 0}}; m1 = Join[#, #] & /@ a {{1, 2, 2, 1, 1, 2, 2, 1}, {3, 4, 4, 3, 3, 4, 4, 3}, {8, 5, 5, 8, 8, 5, 5, 8}} m2 = ConstantArray[#, 3] & /@ m1 {{{1, 2, 2, 1, 1, 2, 2, 1}, {1, 2, 2, 1, 1, 2, 2, 1}, {1, 2, 2, 1, ...


5

Xavier's comment with an additional Partition gives: Partition[Flatten /@ Transpose[{Tuples[a, {2}], Flatten@d}], Length@d] I think this is what the OP had in mind but it doesn't correspond to the next to last row of his "desired result."


5

(Update: fixed incorrectly translated rules) Remember that array indices start at 1 in Mathematica, so you have to adapt your definitions accordingly. Perhaps you could have started there... You can quite literally translate your requirements into conditions and feed them to SparseArray, then use Normal to get a standard representation: ...


5

Here is a solution without the mess of reindexation : NN = 5; c[i_] = If[i == 0 || i == NN, 2, 1]; x[j_] = Cos[j Pi/NN]; result = Table[Switch[{i, j}, {0, 0}, (2 NN^2 + 1)/6, {NN, NN}, -(2 NN^2 + 1)/6, {xy_, xy_}, -x[j] / (2 (1 - x[j]^2)), {_, _}, c[i] (-1)^(i + j)/(c[j] (x[i] - x[j]))], {i, 0, NN}, {j, 0, NN} ]; result // ...


5

The problem is that Dot[a,b] (a and b being atomic, e.g. symbols with no values) evaluates differently than Dot[{a,b},{x,y}] (i.e. the arguments being lists). Dot[{a, b}, x] does not evaluate, so you can transform it using Thread. Dot[{a, b}, {x, y}] does evaluate before it even sees Thread. Dot[{a, b}, {x, y, z}] tries to evaluate and gives up with an ...


5

You can also Apply Join at level 1 to your list: m = RandomInteger[9, {2, 4, 1}] {{{0}, {6}, {7}, {5}}, {{1}, {3}, {8}, {8}}} Join @@@ m {{0, 6, 7, 5}, {1, 3, 8, 8}}


5

If you have a homogenous transformation matrix of the form $$\begin{bmatrix} \mathrm{R_{3 \times 3}} & \mathrm{d}_{3 \times 1} \\ 0_{1\times 3} & 1_{1\times 1} \end{bmatrix}$$ Then the inverse is given by $$\begin{bmatrix} \mathrm{R}^{-1} & -\mathrm{R}^{-1}\mathrm{d} \\ 0 & 1 \end{bmatrix}$$ Therefore, if your ...


4

If you want to compare all to all, use Outer. Say, we have a function h with [[Span]]: h = (#1[[1 ;; -2]] == #2[[1 ;; -2]]) & Note, that -2 suits the list of any length longer than 2. Than, with your data structure: Outer[h, m1[[1]], m2[[1]], 1] // MatrixForm We may get positions of True: pos = Position[Outer[h, m1[[1]], m2[[1]], 1], _?TrueQ] (* ...


4

I think this does what you're asking for. t = 95; M = {{209, 64, 112}, {8, 96, 253}, {65, 200, 95}}; UnitStep[M - t] {{1, 0, 1}, {0, 1, 1}, {0, 1, 1}}


4

The dot product (Dot) in Mathematica is defined as that (discrete) inner product for which the first operation is multiplication and the second, addition. So, you cannot use a dot product directly here, because it cannot incorporate concepts of differentiation or functional application. Instead, you should use a suitably constructed generalized inner ...


4

The formula for spectral norm you are using is meant to be the formal mathematical definition of the quantity. However this is restrictive for practical use as symbolic norm calculation on high dimensions are very cumbersome. The formulation you might be looking for is the following. Here $\mu_{2}$ is the logarithmic two norm. $$\mu_{2}(A) := ...


4

Algebra approach. Dot Wwith a column vector of 1's. W . ConstantArray[1, {Last@Dimensions@W, 1}] Was just curious if Dot approach was faster than Total/@ for large symbolic matrices after reading comments. sqSymMx[m_Symbol, n_Integer?Positive] := Table[Indexed[m, {i, j}], {i, n}, {j, n}]; t = With[{r = sqSymMx[x, #]}, {#, First /@ ...


4

Make j as the function argument for the matrix. mat[j_] := {{2 j, j, -j, 0}, {2, j, -j, -2}, {2, 3, j, -j}, {0, j, -j, 2}}; Then Manipulate works Manipulate[ mat[j] // MatrixForm, {j, 0, 10, ControlType -> Animator, AnimationRate -> 1, RefreshRate -> 10}]


4

m = {{2, 5, 1, 0}, {2, 0, -1, -2}, {2, 3, 4, -3}, {0, 1, -2, 2}}; base[m_] := Transpose[SortBy[Eigenvectors[m] // N, Norm]] diag[m_] := Chop[Inverse@#.m.#]&@base[m] diag[m] // MatrixForm


4

Alternatively to MarcoB's solution (but using their definitions), use a Table and a Which or a Piecewise. This is strictly for the purpose of showing more methods. matrixgenerator[n_Integer] := Module[{x, c}, x[i_] := Cos[i Pi/n]; c[i_] := If[i == 0 || i == n, 2, 1]; Table[ Which[ i == j == 0, (2 n^2 + 1)/6, i == j == n, -(2 n^2 + 1)/6, ...


4

To get the expresions for the Eigenvalues Eigenvalues[{{a, 2} , {3, 4}}] {1/2 (4 + a - Sqrt[40 - 8 a + a^2]), 1/2 (4 + a + Sqrt[40 - 8 a + a^2])} make each expresion Equal to zero (f@x is the Prefix form of f[x]) Thread@Equal[Eigenvalues[{{a, 2} , {3, 4}}], 0] {1/2 (4 + a - Sqrt[40 - 8 a + a^2]) == 0, 1/2 (4 + a + Sqrt[40 - 8 a + a^2]) == 0} ...



Only top voted, non community-wiki answers of a minimum length are eligible