Tag Info

Hot answers tagged

7

Avoiding exact calculation by using approximated numerical value before calculation speeds things up. I'm also calculating only the first Eigenvectors as pointed pot by @Öskå. Now it takes only 15 milliseconds time AbsoluteTiming[Chop@Eigenvectors[N[m], 1, Quartics -> True]] {0.015600, {{-0.0725514, -0.106358, -0.0986766, -0.110735 [...] }}}


7

SetAttributes[f, Listable]; f@__ := 0 f[a_, b_] /; b != 0 := a/b First@AbsoluteTiming[f[c, d];] Edit On the same spirit, this is 50% faster: SetAttributes[g, Listable]; g[_, 0] := 0 g[a_, b_] := Divide[a, b] First@AbsoluteTiming[g[c, d];]


6

Here is one way to see what is happening: z1 = Table[A.({rd[[i]]}\[Transpose].{rd[[i]]}), {i, 1, dim}]; // AbsoluteTiming z2 = Table[(A.{rd[[i]]}\[Transpose]).{rd[[i]]}, {i, 1, dim}]; // AbsoluteTiming which explicitly groups the calculations using parenthesis. As you found, z1 is slower than z2, which makes sense because z1 is essentially the product of ...


6

Simply turn all the 0/0s into 0/1s: ans2 = c/(1 - Sign@d + d); // AbsoluteTiming {0.7630000, Null} The above answer only works for specific example in the question i.e. it only handles positive lists and identical mask. For more general cases one can use: n = 1000; (* a and b contain non-positive elements now *) origin := RandomInteger[{-9, 9}, {n, ...


6

SparseArray can help, given the size and nature of the mask. It's slightly faster to convert c and d to sparse arrays than to convert a and b. mask = SparseArray@RandomChoice[{0, 0, 1}, {n, n}]; First@AbsoluteTiming[ c = mask a; d = mask b; Quiet[foo2 = Block[{Indeterminate = 0}, SparseArray[c] / SparseArray[d]]] ] (* 0.151565 *) Compare: ...


6

Perhaps MatrixLog[ m ] / Log[2]


5

You could try this, which is straightforward: A = {{0.5, 1}, {2, 3}}; MatrixQ[A, IntegerQ] (* False *) Or alternatively - still pretty readable though! ArrayQ[A, _, IntegerQ] (* False *) This has the added bonus of being applicable to other arrays, for example: integerMatrix = RandomInteger[10, {10, 10, 10}]; ArrayDepth@integerMatrix (* 3 *) ...


5

Another way is to use ListConvolve: neighbors = ListConvolve[{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, m, {2, 2}, 0]; neighborCount = ListConvolve[{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, ConstantArray[1, Dimensions@m], {2, 2}, 0]; neighbors/neighborCount == MeanFilter[m, 1] True However, for speed it's not so good to go over the entire matrix in order to ...


5

For example, this removes both rows and columns containing 100 m = matrix[[Sequence @@ (Complement[Range@Length@matrix, #] & /@ Transpose@Position[matrix, 100])]]; m // MatrixForm For removing rows containing 100 you could do (among others): matrix /. {___, 100, ___} :> Sequence[] or DeleteCases[matrix, {___, 100, ___}]


5

Take the 2 by 2 case: {{a11,a12},{a21,a22}}.{x1,x2}={b1,b2}. If x and b are known and a is unknown, you can reformulate the problem to look like this: {{x1,x2,0,0},{0,0,x1,x2}}.{a11,a12,a21,a22}={b1,b2}. Now it is in the form where you can apply Solve or LinearSolve to find the a. Of course the answer will not be unique. In this case an answer is x = {{x1, ...


4

Matrix multiplication is built in in Mathematica. Just use the dot for multiplication. Here are two 2x2 matrices a = PauliMatrix[1] b = PauliMatrix[3] (* Out[49]= {{0, 1}, {1, 0}} *) (* Out[50]= {{1, 0}, {0, -1}} *) Here's a product a.b (* Out[53]= {{0, -1}, {1, 0}} *) and here is the product of the same factors in reverse order b.a (* Out[52]= ...


4

The suggestions bar tries to offer a useful set of follow-up operations based upon the type of a result. In the case at hand, the result is a list of lists. But it so happens that all of the sublists are the same length, which happens to be the representation of a matrix in Mathematica. So the suggestions bar has guessed that the user has a matrix in mind ...


4

You can use CoefficientArrays: xx = {a[0, 0], a[0, 0] + a[1, 0] + a[2, 0] + a[3, 0], a[0, 0] + a[0, 1] + a[0, 2] + a[0, 3], a[0, 0] + a[0, 1] + a[0, 2] + a[0, 3] + a[1, 0] + a[1, 1] + a[1, 2] + a[1, 3] + a[2, 0] + a[2, 1] + a[2, 2] + a[2, 3] + a[3, 0] + a[3, 1] + a[3, 2] + a[3, 3], a[1, 0], a[1, 0] + 2 a[2, 0] + 3 a[3, 0], a[1, 0] + a[1, ...


4

Perhaps this example matrix = RandomInteger[{0, 2}, {3, 3}]; matrix // MatrixForm which displays thiis 0 0 2 1 0 2 0 0 1 Then this matrix /. v_ /; v == 0 -> "" // MatrixForm which displays this 2 1 2 1


3

matrix2x2 (* {{{a1, a2}, {b1, b2}}, {{c1, c2}, {d1, d2}}} *) Map[Hold, matrix2x2, {2}] (* {{Hold[{a1, a2}], Hold[{b1, b2}]}, {Hold[{c1, c2}], Hold[{d1, d2}]}} *) And so: ReleaseHold[Map[Hold, matrix2x2, {2}].Map[Hold, matrix2x2, {2}]] (* {{{a1^2 + b1 c1, a2^2 + b2 c2}, {a1 b1 + b1 d1, a2 b2 + b2 d2}}, {{a1 c1 + c1 d1, a2 c2 + c2 d2}, {b1 c1 ...


3

There are a number of things that are going wrong here, and I'm not sure where to start: You do not have any continuously valued functions. You have simply defined six symbols, x1[0] through x2[2]. You cannot integrate x1[i] or x2[j] with respect to i or j because these symbols are not functions of i and j -- they are only defined for integer values. Maybe ...


3

there is no solution for the system. sol = Solve[res[[;; 3]] == p[[;; 3]]] (*{{x1 -> -3.16146, x2 -> 20.0611, x3 -> -13.5576}}*) res /. sol (*{{{0.}, {0.}, {0.}, {-3.92175}}}*)


3

Another method is to use Array, the fourth parameter of which sets the function that combines expressions: m = RandomReal[9, {3, 3, 3}]; Array[m[[#]] &, 3, 1, Dot] {{606.041, 638.877, 525.972}, {1011.5, 1068.12, 856.671}, {532.56, 556.236, 435.836}} Equivalent to: Dot @@ m {{606.041, 638.877, 525.972}, {1011.5, 1068.12, 856.671}, {532.56, ...


3

My favorite method being the one in @belisarius' answer using Part, or a slight variation of it, (matrix[[##&@@Complement@@@Transpose[{Range@Dimensions@matrix, Transpose@Position[matrix, 100]}]]]), here are a few more, clunkier, alternatives: matrix//MatrixForm pattern = Join @@ ({{#, _}, {_, #2}} & @@@ Position[matrix, 100]); m2 = ...


3

You can use the Postion to exact the positions of 100 tagPos=Position[matrix, 100] (*{{3, 2}, {5, 5}, {6, 7}}*) Then using the Last to achieve the column of 100 Rest/@Position[matrix, 100] (*{{2}, {5}, {7}}*) So lastly, Delete res= Delete[Transpose@matrix, Rest /@ tagPos] // Transpose; MatrixForm@res $$ \left( \begin{array}{cccc} 1 & 3 ...


3

ClearAll[a, b, c, x, y, z, list, func]; list = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}; func[x_, y_, z_, a_, b_, c_] := {a, b, c}.{x, y, z} func[##, a, b, c] & @@ list[[1]] (* a x1+b y1+c z1 *) func[## & @@ #, a, b, c] &@list[[1]] (* a x1+b y1+c z1 *) func[##, a, b, c] & @@@ list (* {a x1+b y1+c z1,a x2+b y2+c z2,a x3+b y3+c z3} *) ...


3

If RowReduce won't help, then perhaps I don't know what you're looking for. Here's my understanding of the question in which I use RowReduce to get the answer. Example A random matrix: SeedRandom[1]; mat = RandomSample[#~Join~Accumulate@RandomSample[#, 2] &@ RandomInteger[{-5, 5}, {35, 37}]]; MatrixRank[mat] (* 35 *) We can use the ...


3

One way is to start with empty matrix. Add the first column. Then loop, each time adding the next column, and checking if the rank of this matrix has increased from before, if so, keep it, else skip over to the next column. Keep doing this until you reach the last column in the original matrix, or have collected m columns, where m is the rank of the original ...


2

For the given example where no values in b are zero before the mask one can use: Divide[mask*a, b] Note the use of explicit Divide for optimum performance. Timings: (* your data *) (r1 = Quiet[c/d /. Indeterminate -> 0]); // AbsoluteTiming // First (r2 = Divide[mask*a, b]); // AbsoluteTiming // First r1 == r2 4.578262 0.212012 ...


2

c/(d /. (0 | 0. -> Infinity));


2

I would approach this specific straightforward example in a straightforward way. Eliminate your matrix using RowReduce[mm]; red=RowReduce[mm]]; red//MatrixForm $\begin{pmatrix} 1 & 0.&0.&0.&0.&0.&0.&0.&0.&0.&\\ 0 & 1 &0.&0.&0.&0.&0.&0.&0.&0.&\\ 0 & 0 &1 ...


2

Change rows and columns to taste. rows = 2; columns = 3; matr = RandomReal[{10^-12, 10^-6}, {rows, columns}]; (matr2 = Table[Row[{ "matrix(", i, ",", j, ") = ", FortranForm[matr[[i, j]]]}], {i, Length[matr]}, {j, Length[matr[[1]]]}] // Flatten) // Column


2

building on @rcollyer's answer: The main improvement here is we print the list of elements in a way that the whole list can be copied at once. (also handle arbitrary dimensions automatically ) SetAttributes[fortranprint, HoldFirst] fortranprint[array_Symbol] := fortranprint[array, SymbolName[Unevaluated[array]]]; fortranprint[array_, name_] := ...


2

There is no subtlety to this, but it works: Table[i j, {i, 5}, {j, 5}] MapIndexed[Print["a(", #2[[1]], "," , #2[[2]], ") = ", #1] &, %, {2}];


2

mat = {{{1, 0.4, 0, 0, 0, 0}, {2, 0.5, 0, 0, 0, 0}, {3, 0.2, 0, 0, 0, 0}}, {{3, 0.2, 1, 5.5686, 23.618, 1}, {2, 0.5, 0, 0, 0, 0}, {1, 0.4, 0, 0, 0, 0}} , {{2, 0.2, 0, 5.5686, 23.618, 1}, {3, 0.5, 0, 0, 0, 0}, {1, 0.4, 0, 0, 0, 0}}}; You can use #[[Ordering[#]]] & /@ mat or Sort /@ mat or Map[Sort, mat] all give (* { {{1, ...



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