Tag Info

Hot answers tagged

8

The key to speedup here is generating the set of random number in one go instead of calling RandomVariate repeatedly. Generally, instead of Table[RandomVariate[...], {size}] use RandomVariate[..., size] It can also generate a multidimensional array of random values in one go. I rewrote your code to do this: out = AbsoluteTiming[Table[ ...


6

Transpose[{t, #}] & /@ Z {{{{129600, 30240}, 1}, {{30240, 10080}, 3}, {{10080, 1440}, 3}, {{1440, 0}, 7}}, {{{129600, 30240}, 8}, {{30240, 10080}, 6}, {{10080, 1440}, 6}, {{1440, 0}, 3}}, {{{129600, 30240}, 3}, {{30240, 10080}, 5}, {{10080, 1440}, 6}, {{1440, 0}, 13}}, {{{129600, 30240}, 15}, {{30240, 10080}, 15}, {{10080, 1440},9}, {{1440, ...


6

Answering my own question per Szabolcs' sugggestion: Answer WRI affirms that SparseArray[__] is broken (in 9.0 and 9.01) WRI's suggested workaround is SparseArray//Normal My experience with this particular SparseArray bug has been that reliable coding requires that the Normal workaround be applied to every instance of SparseArray, because the failures ...


5

I'm assuming here that the OP's code have been evaluated so we can test if the following works. Thanks to george2079 this is the initially populated rows indices list. start = #1 (39 - #1)/2 - 18 & /@ Range[18]; {1, 19, 36, 52, 67, 81, 94, 106, 117, 127, 136, 144, 151, 157, 162, 166, 169, 171} I'm assuming we have rows referring to those ...


5

Assumptions are not used in normal evaluation, but only in certain functions like Simplify. Another, simpler example: In[1]:= $Assumptions = x \[Element] Reals Out[1]= x \[Element] Reals In[2]:= Conjugate[x] Out[2]= Conjugate[x] In[3]:= Simplify[%] Out[3]= x You see, during normal evaluation Mathematica doesn't recognize that x is supposed to be ...


4

L = 10; First of all you could get Booolian array simply as (thx. to @Kuba) RandomInteger[1,{L,L,L}] But if you are interested in thresholding: M = RandomReal[{0, 1}, {L, L, L}]; First of all do not binarize procedurally, use functional style, say: bM = M /. x_ -> UnitStep[x - .5]; or image processing: bM = ImageData[Binarize[Image3D[bM], ...


4

For exact results, use exact input: p = {{7/10, 15/100, 15/100}, {2/10, 8/10, 15/100}, {1/10, 5/100, 7/10}} s = {{7, 0, -2}, {10, -1, 1}, {4, 1, 1}} then (Inverse[s].p.s) // MatrixForm When using floating points, there is always chance of noise generated. Chop[] can be used to clean the final output p = {{.7, .15, .15}, {.2, .8, .15}, {.1, .05, .7}} ...


3

Eigenvectors for inexact arguments are normalized: Eigenvectors[{{1, 4}, {4, 100}}] % // N Normalize /@ %% // N Eigenvectors[{{1.0, 4.0}, {4.0, 100.0}}] (* {{1/8 (-99+Sqrt[9865]),1},{1/8 (-99-Sqrt[9865]),1}} {{0.0403383,1.},{-24.7903,1.}} {{0.0403055,0.999187},{-0.999187,0.0403055}} {{0.0403055,0.999187},{-0.999187,0.0403055}} *)


3

Well, there are a lot of ways. For example: m = ConstantArray[2, {3, 3}]; rule[x_, {i_, j_}] := (x /. x :> x f[ i, j]) MapIndexed[rule@## &, m, {2}] (* {{2 f[1, 1], 2 f[1, 2], 2 f[1, 3]}, {2 f[2, 1], 2 f[2, 2], 2 f[2, 3]}, {2 f[3, 1], 2 f[3, 2], 2 f[3, 3]}} *) Or: Normal@SparseArray[{i_, j_} :> rule[m[[i, j]], {i, j}], Dimensions@m] (* {{2 ...


3

Start with the n x n matrix f[n] given by (for example, belisarius' code): f[n_] := ArrayPad[{{x, y}, {z, x}}, {0, n - 2}, "Fixed"] + DiagonalMatrix[Join[{0}, ConstantArray[u - x, n - 1]]] And evaluate substitutions = { x -> {{0, 1}, {1, 0}}, y -> {{1, 1}, {1, 0}}, z -> {{1, 0}, {1, 1}}, u -> {{p, q}, {r, s}} } If ...


3

You can use Outer for this: result = Outer[Plus, Intensity, Intensity]; Image[Rescale[result]]


3

Another version using Subsets. I assume that the initially defined rows are stored in x, so: x = o[[{1, 19, 36, 52, 67, 81, 94, 106, 117, 127, 136, 144, 151, 157, 162, 166, 169, 171}]]; Then the full matrix is constructed with: o2 = Mean[x[[#]]] & /@ SortBy[Subsets[Range[18], {1, 2}], First]; o2 == o (* True *)


3

Another approach, maybe a little easier to understand: Construct an array of only the initially defined rows: initial = o[[#1 (39 - #1)/2 - 18]] & /@ Range[18] ; this table procedure makes use of the fact that your 'initial' rows are also the "average" of the row with itself..: Transpose@ Table[ Flatten@ Table[ (initial[[j, k]] + initial[[i, ...


2

Apologies if I've misunderstood your questions. I've never used Dispatch before, as suggested by Kuba in the comments, but it seems to be the right tool for the problem if speed is your concern. First off, I hope it's clear that accessing the list corresponding to an index pair is as straightforward is applying the rules list for pattern replacement: rules ...


2

The following assumes that your replacements are 3x3 matrices but it is easy to generalize. I'm not sure if you need. It will take care of -1 on edges. replace[mm_, reps_] := Module[{m = ArrayPad[mm, 1], pos = Position[mm, -1]}, MapThread[ (m[[#[[1]] ;; #[[1]] + 2, #[[2]] ;; #[[2]] + 2]] = #2) &, {pos, reps}]; ArrayPad[m, -1]] mm = ...


2

SortBy[Union[Cases[ArrayRules[L], (r : (_ -> x_ /; Positive[x])) -> r], Cases[ArrayRules[R], (r : (_ -> x_ /; Positive[x])) -> r]], Last] // Reverse This sorts the rules by value of element, reversing it puts larger elements with same index first, default behavior of SparseArray is to ignore later rules with same source. Same idea sans the ...


2

Finally I got a feedback from Wolfram support on the AMD algorithm. It turned out that there is (almost as usual) an undocumented implementation of the AMD algorithm within Mathematica. The algorithm is exactly identical to the MATLAB implementation, thus exactly what I was looking for. By calling SparseArray`ApproximateMinimumDegree[m_Matrix] one gets ...


2

There is an add-on package that's worth mentioning in this context, mainly to address Wouter's comment: in the Combinatorica package, you find some group-theory related commands that are not part of the System context to which the linked guide refers. One of them is ConstructTabelau, addressing the comment by Wouter. Sometimes the built-in gems are hard to ...


2

Your expression simplifies to this $$\vec X \vec X^T A + A^T \vec X \vec X^T$$ using just these rules Unprotect[D, Transpose, Dot]; (*Derivative rules*) D[Tr[A_], X_] := Tr[D[A, X]] D[Transpose[A_], X_] := D[A, X]\[Transpose] D[A_ .B_, X_] := D[A, X].B + A.D[B, X] (*Tranpose rules*) 0\[Transpose] := 0 1\[Transpose] := 1 (A_\[Transpose])\[Transpose] := A ...


2

Well, this gives a polynomial answer: determinante = N@Expand@Det[SetPrecision[M15x15, Infinity]] (* -8.85105*10^55 - 5.50608*10^69 x + 2.79901*10^80 x^2 - 5.66691*10^90 x^3 + 3.30477*10^100 x^4 + 4.27517*10^110 x^5 - ... 7.20261*10^254 x^51 + 4.72779*10^252 x^52 + 6.39829*10^250 x^53 - 4.74757*10^247 x^54 - 4.47661*10^245 x^55 *) The ...


2

I'll illustrate on a smallish matrix, using exact arithmetic. That should make it relatively easy to verify correctness. First we create a tridiagonal 5x5 matrix. n = 5; SeedRandom[33333]; mat = RandomInteger[{-100, 100}, {n, n}]; Do[mat[[i, j]] = 0; mat[[j, i]] = 0, {i, 3, n}, {j, 1, i - 2}]; mat (*Out[68]= {{-30, -98, 0, 0, 0}, {12, 72, 29, 0, 0}, {0, ...


1

Although the documentation could be clearer on this point, Eigenvectors doesn't like to work symbolically on a matrix containing elements with approximate numbers. The solution is to Rationalize the matrix. data = {{1.8741*10^7 + 1.40161*10^6 B, 2.79374*10^7}, {2.79374*10^7, -3.1235*10^7 - 1.40161*10^6 B}}; Eigenvectors[Rationalize[data]] // Column ...


1

This will zero out the negative entries: clip = Clip[#, {0, Infinity}] & rules = Join[Most@ArrayRules@clip[L], Most@ArrayRules@clip[R]]; Warning, the following is undocumented, unsupported, might change in future versions, might blow up your computer, etc. SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Max] ...


1

The matrix Clear[a,B,d,j,M]; m = {{B/2 - d - j/2 + a, (Sqrt[2]*j)/2, M}, {(Sqrt[2]*j)/2, B/2 + a, 0}, {M, 0, (-3*B)/2 - d + j/2 + a}}; has eigenvalues determined by the characteristic polynomial of (maximal) degree 3. In the absence of any other information, we get three Root objects as the eigenvalues, which is Mathematica's way of preserving all ...


1

Seems like the problem is not solved completely in the previous post. For this problem, it is straightforward to obtain the positive semi-definite condition of S by Cholesky decomposition, which all the diagonal elements should be nonnegative. Diagonal[CholeskyDecomposition[S]] After some simplification, it can be shown the diagonal elements are {1/r, ...


1

These are ancient routines I have been using a long time ago. As a matter of fact, it's been so long that I do not even remember if I wrote them or simply shamelessly took them from some other source. Back at the time the only sources I had at my disposal where The Mathematical Journal (prior to 1998 or 1999), Bahder's wonderful book (which is the most ...



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