Tag Info

New answers tagged

1

Having upgraded to Mathematica 10.0.0, it turns out that the Distribution library greatly facilitates the numerical simulation of boson-sampling experiments using the above PermanentCode` package. The built-in function KolgomorovSmirnovTest is particularly valuable; the Wikipedia entry Kolmogorov–Smirnov test provides a good introduction. The appended ...


0

I don't know why you believe your elem function does not work as it is just how I would approach this and it works as expected: elem[x_?MatrixQ, part__: All] := x[[part]]; elem[(a A + a B), 1, 1] /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}} 18


2

Here is one possible way to do it: L1 = {{1, 2}, {1, 3}, {1, 4}}; L2 = {{1, 2}, {1, 3, 2}, {1, 4, 3, 2}}; Outer[Boole[#1 == #2[[{1, -1}]]] &, L1, L2, 1] which produces $$\left( \begin{array}{ccc} 1 & 1 & 1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),$$ which shows that the computation of L2 is redundant, since the ...


1

Since V10 you can play with Inactivate and Activate x = a A + a B /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}} // Inactivate; x[[1]] x[[2]] y = Activate[x, ReplaceAll] z = Activate[y] {{18, 18}, {18, 18}} z[[1, 1]] 18 Or, directly Activate[x][[2, 2]] 18


2

exp = (a A + a B) /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}}; exp[[1, 1]] The original code takes part [[1,1]] of the expression which is a, hence 2. Alternatively you could put parentheses around expression and rules and take part or Part[(a A + a B) /. {a -> 2, A -> {{1, 2}, {3, 4}}, B -> {{8, 7}, {6, 5}}}, 1, ...


2

Although I prefer kguler's method I don't want to be left out of the fun therefore: Cases[triplets, {r_, c1_, c2_} :> matrix[[r, c1 ;; c2]]]


3

you still can use MapThread. consider the example given by kguler: MapThread[matrix[[#, #2 ;; #3]] &, Transpose[triplets]]


4

You can use matrix[[#, #2 ;; #3]] & @@@ triplets A 20 by 10 example: matrix = RandomInteger[10, {20, 10}]; matrix // TableForm triplets = Flatten/@Transpose[{RandomInteger[{1, 20}, {8}], Sort/@RandomInteger[{1, 10}, {8, 2}]}] {{14, 5, 10}, {11, 4, 10}, {2, 2, 10}, {10, 8, 10}, {12, 3, 9}, {3, 3, 5}, {6, 4, 7}, {5, 5, 10}} matrix[[#, #2 ;; ...


1

If I understand you correctly: f[n_, t_] := Select[{#, Eigenvalues@#} & /@ Tuples[{0, 1}, {n, n}], MemberQ[#[[2]], t] &][[All, 1]] But be careful because the tuples thing grows ... quickly :). Finding an efficient way is more complicated.


10

Using Composition I can apply RotationTransform, TranslationTransform , ShearingTransform one after the other. Graphics3D[{ Opacity[1] , Red , Arrow[{{0, 0, 0}, {1, 0, 0}}] , Green , Arrow[{{0, 0, 0}, {0, 1, 0}}] , Blue , Arrow[{{0, 0, 0}, {0, 0, 1}}] , Opacity[0.2] , GeometricTransformation[Cuboid[-{1, 1, 1}/4, {1, 1, 1}/4], ...


1

Starting with: in = {{"A", 10, "D", 1}, {"B", 3, "A", 2}, {"C", 7, "B", 3}, {"D", 6, "C", 4}}; A method using GatherBy: Join @@@ GatherBy[Join @@ Partition[in, {4, 2}][[1]], First] {{"A", 10, "A", 2}, {"B", 3, "B", 3}, {"C", 7, "C", 4}, {"D", 6, "D", 1}} And a method using the new-in-10 Association functionality: Merge[{# -> #2, #3 -> #4} ...


4

RandomVariate[NormalDistribution[], {r, r}] does not give you normalized eigenvectors. To obtain an orthonormal basis, you can first generate a random matrix, and then apply Orthogonalize to it. Following is the correct code. r=4;(*matrix dimension*) dom={1,10};(*domain of random numbers*) eig=DiagonalMatrix[-RandomInteger[dom,r]] (*eigenvalues in diagonal ...


4

The magical words are Singular Value Decomposition. The singular vectors corresponding to small singular values form the kernel. Of course, Singular Value Decomposition is available in Mathematica as SingularValueDecomposition[]. As confirmed by Daniel Lichtblau, the built-in Tolerance option to NullSpace[] does it this exact way.


12

You can use HoldForm or Defer with Composition if you are still using Pre V10 versions: MatrixForm[Inner[Composition[Defer, Times], A, A, Composition[Defer, Plus]], TableSpacing -> {3, 3}] MatrixForm[Inner[Times, A, A, Composition[HoldForm, Plus]], TableSpacing -> {3, 3}] MatrixForm[Inner[Times, A, A, Plus], ...


22

If you have Mathematica 10 you can use the new Inactive functionality step1 = MatrixForm[Inner[Inactive[Times], A, A, Inactive[Plus]], TableSpacing -> {3, 3}] step2 = Activate[step1, Times] Activate[step2]


2

Clear[A, n, k, nn, aa] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Print["A"] MatrixForm[A] aa = Length[A]; Print["First step in matrix multiplication A times A"] MatrixForm[ Table[Flatten[ Table[Table[ StringJoin[{"(", ToString[A[[nn, k]]], ")", "\[CenterDot]", "(", ToString[A[[k, n]]], ")", If[k < aa, "+", If[n == aa, "", ","]]}], ...


1

Unless I misunderstand you I think this question is probably a bit too simple to remain open, but nevertheless: We can compute the symbolic sum: expr = Sum[c^(i + j - 2 k) (-1)^(-k + j) (i*j)/((i - k)! (j - k)! k!), {k, 0, Min[i, j]}] ((-1)^j c^(i + j) (c^2)^-i HypergeometricU[-i, 1 - i + j, c^2])/((-1 + i)! (-1 + j)!) Then make a table: Table[expr ...


1

Table[Sum[ c^(i + j - 2 k) (-1)^(-k + j) (i*j)/((i - k)! (j - k)! k!), {k, 0, Min[i, j]}], {i, 1, 5}, {j, 1, 5}]


1

LinearSolve If you have a proper dimensions I recommend you to use LinearSolve here. Let us take 4 random matrices as a basis (complex matrices for generality) basis = RandomComplex[1 + I, {4, 2, 2}]; MatrixForm /@ basis The target matrix T = RandomReal[1, {2, 2}]; T // MatrixForm We can treat 2x2 matrices as vectors with 4 elements, Flatten them ...


1

Given a matrix: eta = ({{1, 0}, {0, -1}}); one can decompose this into three matrices in the following way: {u, w, v} = SingularValueDecomposition[eta]; (* Where the original eta is defined by: *) u.w.Transpose[v] (* = eta *) Another way, which more appropriately addresses your problem is to use Schur decomposition. eta = ({{1, 0}, {0, -1}}); {q, t} ...


3

The code as below can achieve the result that you need. Clear@t; mat = RandomInteger[{1, 10}, {20, 4}]; Evaluate[t /@ Range[Length@mat]] = mat Because of (=)Set has the attribute HoldFirst, I use the function Evaluate to evaluate first before proceeding with Set.


3

Perhaps this will work for you. Given a symbol, say t, and a matrix, say m, it will define a set of indexed variables t[1], t[2], ..., t[n], bound to the respective rows of m. SetAttributes[assign, HoldFirst]; assign[name_Symbol, matrix_List] := ( Clear @ name; Do[t[i] = matrix[[i]], {i, 1, Length@matrix}] ) Generate some data. SeedRandom[42]; m ...


2

You can generate such matrices using the following code: n = 8; MatrixForm[ Table[Piecewise[{{α, i == j}, {β, Abs[i - j] == 1 || Abs[i - j] == n - 1}}], {i, n}, {j, n}]] which generates $$\left( \begin{array}{cccccccc} \alpha & \beta & 0 & 0 & 0 & 0 & 0 & \beta \\ \beta & \alpha & \beta & 0 & 0 ...


2

If you are going to work with non-commutative algebras like the matrix multiplication I recommend you try the NCAlgebra package. << NC` << NCAlgebra` (2 a) ** (3 b) 6 a ** b P.S. In NCAlgebra all lowercase variables are non-commutative by default.


1

Given a matrix mat replace the 0s in mat with an expression that depends on the indices. randommatrix = RandomInteger[1, {6, 6}]; randommatrix // MatrixForm MapIndexed[# /. {(0) -> Quiet@Style[Evaluate[diff2 @@ #2], Red], _ :> 0} &, randommatrix, {2}] // MatrixForm mat = 1 - Unitize[tttt2]; args = Table[{w, Pprobe}, {w, 2.5, 3., 0.1}, ...


3

Update Another try arrow = Graphics[{Arrowheads[Small], Arrow[{{0, 0}, {6, 0}}]}, ImageSize -> {50,10}]; product[m_, n_] := Module[{s, t}, {{Subscript[r, n]/m[[n, n]]}, t = MapAt[#/m[[n, n]] &, m, n], Table[ s = Subscript[r, i] - t[[i, n]] Subscript[r, n]; t = MapAt[# - t[[i, n]] t[[n]] &, t, i]; s, {i, n + 1, Length[m]}], t} ...


7

This is not a matter of "force". A Root object represents the exact root of a polynomial that cannot be represented exactly in closed form, or (in the case of cubics and quartics) cannot be represented succinctly. It is not "unsolved"; it is just a way of writing something that is hard or impossible to write down otherwise. If you want to convert cubic or ...


4

Let me reconstruct your Vandermonde matrix: A = Outer[Power, Range[10], Range[0, 3]]; A // MatrixForm Then there are two possibilities to compute SVD: Analytic expressions Numerical values Your input is the integer array so Mathematica choose the first one and try to give your the answer in an analytic form. It returns the result as roots of the ...


1

Just do this. A = {{1, 1, 1, 1}, {1, 2, 4, 8}, {1, 3, 9, 27}, {1, 4, 16, 64}, {1, 5, 25, 125}, {1, 6, 36, 216}, {1, 7, 49, 343}, {1, 8, 64, 512}, {1, 9, 81, 729}, {1, 10, 100, 1000}} // N; {U, S, V} = SingularValueDecomposition[A]; U // MatrixForm S // MatrixForm You can confirm the results. U.S.Conjugate[Transpose[V]] // Rationalize ...


3

Table and MapAt with Span can reduce the code to almost two lines: n = 3; A = RandomInteger[10, {n, n}]; MatrixForm[A] LU = Join[A, IdentityMatrix[n], 2]; res = Table[{LU = MapAt[#/LU[[k, k]] &, LU, k], LU = MapAt[# - #[[k]] LU[[k]] &, LU, k + 1 ;;]}, {k, n}]; Map[augmentedMatrixForm, res, {2}] // Grid With "tags" it is a bit longer LU ...


3

Here are a couple of ways: Cases[mat, {e__, l_} :> {e, l /. rules}] or ReplacePart[mat, {i_, 3} :> (mat[[i, 3]] /. rules)]


4

mat[[All, -1]] = Transpose[mat][[-1]] /. rules; mat {{2, 1, 1000}, {3, 0, 11}, {4, 2, 11}, {2, 0, 33}, {1, 0, 1000}, {0, 0, 11}, {3, 3, 22}, {0, 2, 11}, {0, 1, 11}, {2, 1, 22}}


1

data = RandomInteger[4, {10, 3}]; rules = {0 -> 1000, 1 -> 11, 2 -> 22, 3 -> 33, 4 -> 44}; data /. {a__?NumberQ, b_} :> {a, b /. rules} // MatrixForm


3

If the rule doesn't fit, change it: rules = {a_, b_, #} :> {a, b, #2} & @@@ {0 -> 1000, 1 -> 11, 2 -> 22, 3 -> 33, 4 -> 44}; mat /. rules {{4, 4, 1000}, {4, 1, 1000}, {1, 2, 33}, {3, 1, 1000}, {2, 0, 33}, {4,3, 33}, {2, 1, 11}, {2, 0, 44}, {3, 3, 22}, {1, 2, 22}}


2

With[{m = Transpose[mat]}, Transpose[Append[Most[m], Last[m]/. rules]]]


6

mat = {{2, 1, 0}, {3, 0, 1}, {4, 2, 1}, {2, 0, 3}, {1, 0, 0}, {0, 0, 1}, {3, 3, 2}, {0, 2, 1}, {0, 1, 1}, {2, 1, 2}}; rules = {0 -> 1000 ,1-> 11, 2-> 22, 3 -> 33, 4-> 44}; mat[[All, -1]] = mat[[All, -1]] /. rules; mat (* {{2, 1, 1000}, {3, 0, 11}, {4, 2, 11}, {2, 0, 33}, {1, 0, 1000}, {0, 0, 11}, {3, 3, 22}, {0, 2, 11}, {0, 1, ...


1

Another variant: deleteConsecutive = Split[#][[All, 1]]& deleteConsecutive@{TCC12, TCC12, B96, TM12, TCC12, B96} (* {TCC12, B96, TM12, TCC12, B96} *)


1

f = #1 & @@@ Split @ # &; x = {TCC12, TCC12, B96, TM12, TCC12, B96}; f@x (* {TCC12,B96,TM12,TCC12,B96} *) m = {{1, 1, 1, 1}, {2, 2, 0, 2}, {0, 1, 0, 0}, {2, 2, 0, 0}}; f /@ m (* {{1},{2,0,2},{0,1,0},{2,0}} *)


5

x = {TCC12, TCC12, B96, TM12, TCC12, B96}; x /. {a___, b_, b_, c___} :> {a, b, c} {TCC12, B96, TM12, TCC12, B96} First /@ Split[x] Same output, but probably faster. Update thanks to Belisarius (m = {{1, 1, 1, 1}, {2, 2, 0, 2}, {0, 1, 0, 0}, {2, 2, 0, 0}}) // MatrixForm Map[First, Split /@ m, {2}] // MatrixForm


9

Grid directly supports such lines, called Dividers: m = augmentedMatrix[6]; g = Grid[m, Dividers -> {7 -> {Red, Dashed}}] All that remains is to incorporate the large ( ) brackets used by MatrixForm: MatrixForm[{{g}}] Another approach is to realize that both MatrixForm and Grid produce a GridBox expression: Shallow[ToBoxes @ MatrixForm[m], ...


6

SeedRandom@0; mat = RandomInteger[{0, 10}, {2, 3, 3}]; MatrixForm@List@Grid[List@(TableForm /@ mat), Dividers -> {2 -> Directive[Red, Dashed]}] Of course you can play with spacings: TableForm[#, TableSpacing -> {1, 1}] & /@ mat To go a bit further with Dividers: SeedRandom@0; mat = RandomInteger[{0, 10}, {10, 3, 3}]; ...


4

The OP's listParametricPlot3D constructs nonplanar quadrilaterals (Polygons) for a GraphicsComplex with Polygon[Flatten[ Table[{1 + i + xx j, 2 + i + xx j, 2 + i + xx (j + 1), 1 + i + xx (j + 1)}, {j, 0, yy - 2}, {i, 0, xx - 2}], 1]] where xx, yy are the dimensions of the tensor grid for the surface in the OP's data. One problem with nonplanar ...


10

Here's an approach without If or For. First a helper function: (* Thanks to Belisarius for the mrow& suggestion *) g[x_] := NestWhile[mrow&, x, MemberQ[x - #, 0] &] Then: NestList[g, mrow, 9] // MatrixPlot Where mrow is as you've defined it in the question.


1

row := Take[ NestWhile[Join[#, ConstantArray[RandomInteger[{0, 2}], RandomChoice[{2, 4, 6, 8}]]] &, {} , Length@# < 10 &], 10] Nest[Module[{b}, (While[Times @@ (#[[-1]] - (b = row)) == 0]; Append[#, b])] & , {row} , 9] // MatrixPlot


3

Nice question. I'd try to do something with recursion instead, like Clear[f]; f[n_] := f[n] = newRow[f[n - 1]] f[0] = ConstantArray[10, 10]; newRow[previousRow_] := With[{row = mrow}, If[MemberQ[previousRow - row, 0], newRow[previousRow], row] ] Array[f, 10] // MatrixPlot


5

Could look for a counterexample using FindInstance. tmat = {{t11^2, t11*t12}, {t11*t12, t12^2 + t22^2}} - IdentityMatrix[2]; evals = Eigenvalues[tmat]; FindInstance[(evals[[1]] <= 0 || evals[[2]] <= 0) && t11^2 >= 1 && t22^2 >= 1, Variables[tmat], Reals] (* Out[237]= {{t11 -> Sqrt[2], t12 -> 1, t22 -> ...


5

The eigenvalues are Eigenvalues[{{t11^2, t11*t12}, {t11*t12, t12^2 + t22^2}} - IdentityMatrix[2]] giving {1/2 (-2 + t11^2 + t12^2 + t22^2 - Sqrt[ t11^4 + 2 t11^2 t12^2 + t12^4 - 2 t11^2 t22^2 + 2 t12^2 t22^2 + t22^4]), 1/2 (-2 + t11^2 + t12^2 + t22^2 + Sqrt[ t11^4 + 2 t11^2 t12^2 + t12^4 - 2 t11^2 t22^2 + 2 t12^2 t22^2 + ...


4

I think the analogue of Matlab's implicit concatenation is ArrayFlatten, but it still needs to be called explicitly. (aa = Table[a[i, j], {i, 3}, {j, 3}]) // MatrixForm $$\left( \begin{array}{ccc} a(1,1) & a(1,2) & a(1,3) \\ a(2,1) & a(2,2) & a(2,3) \\ a(3,1) & a(3,2) & a(3,3) \\ \end{array} \right)$$ (bb = Table[{b[i]}, ...


2

You can use the option ZeroTest as follows: mat = {{c1 d1 - e1 f1, c1 d2 - e1 f2, c1 d3 - e1 f3, c1 d4 - e1 f4}, {c2 d1 - e2 f1, c2 d2 - e2 f2, c2 d3 - e2 f3, c2 d4 - e2 f4}, {c3 d1 - e3 f1, c3 d2 - e3 f2, c3 d3 - e3 f3, c3 d4 - e3 f4}, {c4 d1 - e4 f1, c4 d2 - e4 f2, c4 d3 - e4 f3, c4 d4 - e4 f4}, {c5 d1 - e5 f1, c5 d2 - e5 f2, ...


1

Sure, e.g.: TR[DiracMatrix[mu,nu,rho,si,rho,nu,mu,si, Dimension -> n]] gives -4*(-2 + n)^3*n



Top 50 recent answers are included