New answers tagged

1

The matrix desired is at most rank two and can be easily expressed as the product of a matrix and its transpose. I will number the elements for convenience and assume that state > 1 (otherwise we have imaginary elements, which would require further thought). n = 3; v1 = Prepend[Array[a, n], 1]; v2 = Prepend[Array[0 &, n], Sqrt[state - 1]]; M1 = ...


2

I think try to create a zero-matrix and add your matrix on it will be helpful, just like the following code shows. In[22]:= kglobal2 = Module[{sum = ConstantArray[0, {8, 8}]}, sum[[1 ;; 4, 1 ;; 4]] += k12; sum[[{1, 2, -2, -1}, {1, 2, -2, -1}]] += k14\[Transpose]; sum ] Out[22]= {{9.92, 4.08, -6.08, -1.52, 0, 0, -3.84, -2.56}, {4.08, 380450., -1....


1

Will this a-bit-complex code fullfill your need? arrayinsert[mat_, x_, y_] := With[{pre = ArrayFlatten@List@Riffle[#, Unevaluated@ConstantArray[0, {Length@#[[1]], x}]] & /@ mat}, ArrayFlatten[List /@ Riffle[pre, Unevaluated@ConstantArray[0, {y, Length@pre[[1, 1]]}]]]] ArrayPad[k12, {0, 4}] + arrayinsert[Partition[k14, {2, 2}], 4, 4] The key of ...


3

Use Part to subtract intensity and Transpose to align with the wavelength data: wavelength = Range[350, 750, (400/3647)]; withMagnet = Transpose[{wavelength, RandomReal[1, 3648]}]; withoutMagnet = Transpose[{wavelength, RandomReal[1, 3648]}]; (*The above code just simulates your imported data*) diff = Transpose[{withoutMagnet[[All, 1]], ...


6

yourlist = {a, b, c}; mat = ReplacePart[Outer[Times, #, #]&@Prepend[yourlist, 1], {1, 1} -> state] Change yourlist to the list of your interest. Or, taking @mikado 's idea of symbolically diagonalizing: matfunc[1, 1] = state; matfunc[1, a_Integer] := yourlist[[a - 1]]; matfunc[a_Integer, 1] := yourlist[[a - 1]]; matfunc[a_Integer, b_Integer] := ...


3

With Times: avec = Array[a, 4]; m = Outer[Times, avec, avec] /. {a[1] -> 1}; m[[1, 1]] = s; MatrixForm[m] Eigenvalues[m] Here a[2] is your a, a[3] is your b, etc. Change the 4 to 900 if you wish. There are two nonzero eigenvalues.


0

Try this: e = t (2 t^4 + t^2) + 27 t^2; mat = {{e, e}, {e, e}}; Collect[mat, t] {{27 t^2 + t^3 + 2 t^5, 27 t^2 + t^3 + 2 t^5}, {27 t^2 + t^3 + 2 t^5, 27 t^2 + t^3 + 2 t^5}} You can apply FullSimplify to each coefficient for a power of t by supplying it as 3rd argument to collect: MatrixForm[Collect[T4[t], t, FullSimplify]]


0

As far as I understood from your answers to comments you like KronekerDelta in the stiffness tensor, but not in the stress tensor. You can do as follows: stiff[i_, j_, k_, l_] := L KroneckerDelta[i, j] KroneckerDelta[k, l] + \[Mu] (KroneckerDelta[i, k] KroneckerDelta[j, l] + KroneckerDelta[i, l] KroneckerDelta[j, k]) vg = Array[v, {3, 3}]; vg //...


2

Reference: Part ListLinePlot[{myMatrix[[All,1]],myMatrix[[All,2]], ...}] data = {{1, 5, 10}, {1, 5, 10}, {1, 5, 10}, {1, 5, 10}, {1, 5, 10}}; ListLinePlot[{data[[All, 1]], data[[All, 2]], data[[All, 3]]}]] Or for a compact, universal solution (Courtesy of J.M.) ListLinePlot[Transpose[myMatrix]]


3

I think for this kind of matrix it is better to use some of the dedicated matrix formats, like, "MTX" (of Matrix Market) or "HarwellBoeing". Below are two examples using "MTX": one with a dense 6000x6000 matrix and one with a sparse matrix. Dense matrix Mathematica mat = RandomReal[{0, 1}, {6000, 6000}]; Export["/path/RandomMat.mtx", mat, "MTX"] Python ...


3

SetDirectory[NotebookDirectory[]] (mat = RandomReal[1, {5, 5}]) // MatrixForm Export["mat.txt", mat, "CSV"] Open python and type >python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 >>> f = open ( 'mat.txt' , 'r') >>> mat = [ map(float,line.split(',')) for line in f ] >>> print(mat) [[0....


1

change your last two lines to: cT[{_, a2_, a3_}] := c11[m, n, a2, a3, F3, b]; cT /@ centroidList (*{80.1567, -95.8522, 0., 0., -80.1567, 95.8522, 0., 0.}*) Is this what you want?


2

I'll give two solutions here: a not-that elegant solution which can apply to multiple sizes but all blocks MUST be Fully filled! (Just the limitations of Morphological methods) Function[{n}, Pick[mat, #, n] /. {} -> Nothing] /@ Range@Max@# &@ MorphologicalComponents[mat, Method -> "BoundingBox", CornerNeighbors -> False] This use ...


3

Here is a neat way: Array[Function[n, {{n, 7 n - 2, 3, 2 n - 1}, {3, 4, n, 6 n - 1}, {9, 3, n, n - 3}, {n - 2, 1, 8, 7 n/3}}], 10, 1, Dot]


1

You can do j=10; mat = Table[{{n, 7 n - 2, 3, 2 n - 1}, {3, 4, n, 6 n - 1}, {9, 3, n, n - 3}, {n - 2, 1, 8, 7 n/3}}, {n,j}]; Dot @@ Table[mat[[i]], {i,j}]//N Where j is the order to which you want to calculate the product, this gives as answer: {{7.28971*10^13, 1.09566*10^14, 7.92596*10^13, 2.71886*10^14}, {8.76822*10^13, 1.31681*10^14, 9....


7

extractBDM[mat_?MatrixQ] := Pick[mat, #, 1] & /@ Values[ComponentMeasurements[ Image[MorphologicalTransform[Unitize[mat], "BoundingBoxes"]], "Mask", CornerNeighbors -> False]] /. {} -> Nothing Explanation I just treat the values as an image pixel.So our purpose is finding the connected component.Supose your list mat is ...


5

{a, b, c, x, y, z} = RandomReal[{0, 1}, {6, 2, 2}] bk = Dot @@@ Transpose@{{a, b, c}, {x, y, z}} jd = MapThread[Dot, {{a, b, c}, {x, y, z}}] bk == jd (*True*) One can also use Inner[Dot, {a, b, c}, {x, y, z}, List] but you need to wrap lists in Unevaluated: Inner[Dot, Unevaluated@{a, b, c}, Unevaluated@{x, y, z}, List] See here for explanation.


9

MapThread[Dot, {{A, B, C}, {X, Y, Z}}]


3

I am not sure if this is what you want (it would be helpful if you can give an example in your question). What I am doing here is to get the block matrix at any given position. First I create a general matrix. a = RandomInteger[{1, 9}, {2, 2}]; b = RandomInteger[{1, 9}, {3, 4}]; c = RandomInteger[{1, 9}, {2, 2}]; m = SparseArray[{Band[{1, 1}] -> a, Band[...


0

I use subscripted variables extensively in analysis of summations etc (e.g. derivation of Cramer-Rao lower bounds for cases with large numbers of variables). As a very simple example, find the value of m that minimises the following sum. sum = Sum[(Subscript[x, i] - m)^2, {i, 1, n}] eqn1 = D[sum, m] == 0 eqn2 = eqn1 /. u_Sum :> (u /. Subscript[x, i] -&...


3

Your operation does not work because in this case it is not a matricial multiplication but a composition. Try mM = {{D[#, x] &, D[#, y] &}, {D[#, y] &, D[#, z] &}} v = {f[x, y, z], g[x, y, z]} apply[a_, b_] := Inner[#1[#2] &, a, b] This answer come from the article http://www.mathematica-journal.com/issue/v8i4/tricks/contents/...


3

Specify Eigensystem[h, 2, Method -> "Direct"] for both cases. That sparse array may go to a different solver (iterative) than the dense method. You can find more options in the documentation of Eigensystem under the options section.


0

As shown in How to plot vectors in Mathematica, the desired graphics primitive is Arrow with a list of two vectors, one the origin and one the head of the vector. If A is your matrix, this expression builds the list of Arrow primitives. In[1]:= A=Array[a,{2,3}]; Map[Arrow, Transpose[{0 A, A}, {2, 3, 1}]] Out[2]:= {Arrow[{{0, 0}, {a[1, 1], a[2, 1]...


0

Using SparseArrays EK1 = {{a11, a12}, {a21, a22}}; EK2 = {{b22, b23}, {b32, b33}}; {sEK1, sEK2} = SparseArray[#, {3, 3}, 0] &@(Thread[#[[2 ;;]] -> #[[1]][Sequence @@ #[[2 ;;]]], 2] &@(ToExpression@ Characters@ToString[#]) & /@ (Flatten@#)) & /@ {EK1, EK2}; (sEK1 + sEK2) // Normal {{a[1, 1], a[1, 2], 0}, {a[2, 1]...


0

How about mA = RandomReal[NormalDistribution[], {3000, 3000}]; mB = RandomReal[NormalDistribution[], {3000, 3000}]; mC = RandomReal[NormalDistribution[], {3000, 3000}]; Timing[vec0 = Diagonal[mA.mC.mB];] (*{7.515625, Null}*) Timing[Total[Total[mA Transpose[mC.mB], {2}] - vec0]] (*{3.968750, 3.8557602 10^-10}*)


4

Update-2: More compact version proposed by J. M. with the matrix normalized: steps = 3; (*order = 2^step*) h = Nest[Join[KroneckerProduct[#, {1, 1}], KroneckerProduct[IdentityMatrix[Length[#]], {1, -1}]] &, {{1}}, steps]; Orthogonalize[h] // MatrixForm Update-1: Creates un-normalized Haar matrix for $\text{order} = 2^n$. steps = 3; ...


4

I gave a presentation on one (efficient) way to do it at the 2009 tech conference. The essence is this function: matrixAssembly[ values_, pos_, dim_] := Block[{matrix, p}, System`SetSystemOptions[ "SparseArrayOptions" -> {"TreatRepeatedEntries" -> 1}]; matrix = SparseArray[ pos -> Flatten[ values], dim]; System`SetSystemOptions[ "...


2

In line with suggestion from @happy fish, I would write Energy = 1/2 (d1^2/2 + (-(d1/Sqrt[2]) + d2/Sqrt[2])^2) k - d1 P - d2 P - (d1^2/(2 L^2) + d2^2/(2 L^2)) P; d = {d1, d2}; ca = CoefficientArrays[Energy, d]; The list ca contains the term you are seeking, and satisfies ca[[1]] + ca[[2]].d + d.ca[[3]].d == Energy // Simplify Note that ca[[3]] is not ...


1

If we ignore random sampling, this is a simple question of Boolean satisfiability: Module[{m, n, greens, matrix, sol}, m = 8; n = 5; greens = 20; matrix = Array[c, {m, n}]; sol = matrix /. First@FindInstance[ And @@ (BooleanCountingFunction[Length@# - 2, Length@#] @@ # & /@ matrix) && And @@ (...


1

I think what you want is ExpToTrig: matrix = {{1, 1, -1, -1}, {I k, -I k, -q, q}, {E^(I k a), E^(-I k a), -E^(-q b + I g (a + b)), -E^(q b + I g (a + b))}, {I k E^(I k a), -I k E^(-I k a), -q E^(-q b + I g (a + b)), q E^(q b + I g (a + b))}}; FullSimplify[ExpToTrig[Det[matrix]]] 4 I E^(I (a + b) g) (2 k q (Cos[(a + b) g] - Cos[a k] ...


2

Here's a quick-and-dirty method that does what (I think) you're after: makearr[{n_, m_}, p_] := Module[{base = PadLeft[ConstantArray[1, Round[n m p]], n m], cand}, While[(cand = ArrayReshape[RandomSample@base, {n, m}]; Max[Total[cand]] > n - 2 || Max[Total /@ cand] > m - 2)]; Position[cand, 1]]; Example usage: makearr[{5, 5}, .4] {{...


0

Make a list of {eigenvalues, eigenvectors} and sort it with respect to the eigenvalues. A = {{1.999, 0.000428712*I, 0}, {-0.000428712*I, 2.00072, 0}, {0, 0, -4.00057}}; {eigs, vecs} = Eigensystem[A]; list1 = Partition[Riffle[eigs, vecs], 2]; list2 = Sort[list1, #1[[1]] < #2[[1]] &]; list2 // MatrixForm you get


0

We can surely generalize this (to include scalar multiples, for instance), but here's an implementation that factors the expression written in terms of Dots: dotFactor[expr_] := expr //. { Dot[x__, z1_] + Dot[x__, z2_] :> Dot[x, z1 + z2], Dot[z1_, x__] + Dot[z2_, x__] :> Dot[z1 + z2, x], Dot[1, x__] | Dot[x__, 1] :> Dot[x] } Then, using ...


2

Like J.M. said, the use of ListConvolve can be a bit tricky, because by default it cuts overhangs and has no padding. From your description i guess you are more interested in something like this: MatrixPlot[ ListConvolve[GaussianMatrix[10], BoxMatrix[20] - DiamondMatrix[20], {1, -1}, {0, 0}] ] $\mspace{172mu}\Big\Updownarrow$ $\mspace{30mu}$ This ...


3

To verify that the rotations happen the way they're supposed to according to the documentation for EulerMatrix, you could use the following Manipulate: Clear[arrowAxes]; arrowAxes[arrowLength_: 1] := Map[{Apply[RGBColor, #], Arrow[Tube[{-#, #}]]} &, arrowLength IdentityMatrix[3]] Manipulate[ Graphics3D[{GeometricTransformation[arrowAxes[.7], ...


2

Use Simplify with Assuming: m = {{E^(I*β1 + I*β3) Cos[β2], E^(I β1 - I*β3) Sin[β2]}, {(-E^((-I) β1 + I*β3)) Sin[β2], E^((-I) β1 - I*β3)*Cos[β2]}}; MatrixForm[ Assuming[{β1, β2, β3} ∈ Reals, Simplify@ConjugateTranspose[m]]] $$\left( \begin{array}{cc} e^{i \text{$\beta $1}+i \text{$\beta $3}} \cos (\text{$\beta $2}) & e^{i \text{$\...


2

I suppose you would like something like this? Select[A.B.Transpose[R].R.T, FreeQ[#, R] &] This mainly used the expression's tree like property. Due to Dot's Flat Property, the whole expression will actually looks like Dot[A,B,Transpose[R],R,T], thus this code can generate the result you want: A.B.T Note that I've change your C to T cause C is a ...


5

Here's something even more compact than my proposal in the comments: Standardize[RandomReal[1, {4, 4}], 0 &, Total] If you must have a left stochastic matrix where all the entries should be greater than a set value, you can do rejection sampling: keep generating a matrix as long as the smallest value is smaller than the cutoff: While[Min[sm = ...


1

Here is my idea make[n_] := ConstantArray[0.1, {n, n}] + ( (1 - 0.1 n) #/Total[#, {2}] &[RandomReal[{0, 1}, {n, n}]]) Because of the minimum you specified for each entry, this works for $n<11$ only. Did you consider letting the minimum value dependent on $n$ in a decreasing manner?


2

You can directly use the function pauliReduce that I defined in this answer: a = {a1, a2, a3} (* ==> {a1, a2, a3} *) a.{σ[1], σ[2], σ[3]} (* ==> a1 σ[1] + a2 σ[2] + a3 σ[3] *) pauliReduce[ MatrixExp[I a.{σ[1], σ[2], σ[3]}]] $$\frac{1} {\sqrt{\text{a1}^2+\text{a2}^2+\text{a3}^2}}\left(\hat{1} \sqrt{\text{a1}^2+\text{a2}^2+\text{a3}^2} \cos ...


5

One thing you might want to do is write the vector $\vec{a}$ as some magnitude $\theta$ times a unit vector {a1, a2, a3}. Then calculate $\exp(i \theta \,a\cdot\sigma)$. This will allow you to get rid of those factors of $\sqrt{a_1^2+a_2^2+a_3^2}$, which are just some $\theta$ anyway. Here it is in Mathematica:a = \[Theta] {a1, a2, a3}; b = a.Array[...


3

For the first part, you can use MatrixExp. Since you are interested in trigonometric form, I would suggest using polar coordinates from the beginning. p0 = PauliMatrix[0]; p1 = PauliMatrix[1]; p2 = PauliMatrix[2]; p3 = PauliMatrix[3]; a1 = a Sin[q1] Cos[q2]; a2 = a Sin[q1] Sin[q2]; a3 = a Cos[q1]; m = a1 p1 + a2 p2 + a3 p3; m1 = MatrixExp[I m] // ...


0

ClearAll[f] f = Module[{m = #, r = #2, x = #3}, (m[[#, #2 ;;]] = r[[#2 ;;]]) & @@@x; m] &; f[ap, b, z]


5

Here's one way: ap = ConstantArray[0.001, {5, 10}]; b = {0.001, 0.005, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1, 0.5, 1}; z = {{1, 3}, {2, 2}, {5, 7}}; f[ap_, {z1_, z2_}] := ReplacePart[ap, {z1, i_ /; i >= z2} :> b[[i]]] Fold[f, ap, z] (* {{0.001, 0.001, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1, 0.5, 1}, {0.001, 0.005, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1,...


2

Table[Table[ap[[z[[i, 1]], j]] = b[[j]], {j, z[[i, 2]], 10}], {i, 3}]; ap {{0.001, 0.001, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1, 0.5, 1}, {0.001, 0.005, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1, 0.5, 1}, {0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001}, {0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0....



Top 50 recent answers are included