Tag Info

Hot answers tagged

15

Use ArrayRules[]: m = SparseArray[{2, 2} -> i]; mc = SparseArray[ArrayRules[m] /. i -> -i, Dimensions[m]]; MatrixForm[mc] $\begin{pmatrix}0&0\\0&-\mathtt{i}\end{pmatrix}$


13

J. M. has shown you a workaround using ArrayRules and as others mentioned, using Conjugate is more prudent. However, to answer your primary question — "Why doesn't ReplaceAll work on SparseArray?", it is because SparseArray is atomic. In other words, SparseArray objects are "indivisible" and the data contained in them can only be accessed in specific ways ...


13

array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> array0[[#]] & /@ {1, 2, 3}, {5, 5}]; array1 // MatrixForm Update: Generalizing for arbitrary matrix input: rttF = Function[{mat}, With[{dims = Dimensions[mat]}, SparseArray[Band[{# - 1 + Last@dims, #}, ...


13

I found a way to dramatically improve the performance of this algorithm by using the undocumented function SparseArray`KrylovLinearSolve. The key advantage of this function is that it seems to be a near-analog of MATLAB's pcg, and as such accepts as a first argument either: a square matrix, or a function generating a vector of length equal to the length ...


12

As Silvia notes in the comments, this behaviour is because of the predictive interface that is new in version 9. Mathematica tries to inspect the contents of the output to determine which contextual menu options to show in the suggestions bar depending on whether it is an array of integers/reals/mixed or if it is square/rectangular, etc. My guess is that ...


11

You can always modify the matrix so that the most negative eigenvalue is also the one with the largest absolute value, and hence corresponds to the first in the list returned by EigenVectors. An upper bound for the largest absolute value of any eigenvalue is the Hilbert-Schmidt norm. So you can rescale your matrix by subtracting this norm times the unit ...


10

This question came up in Chat the other day. Here is the solution I proposed. banded[n_Integer?EvenQ] := With[ {main = RandomReal[99, n - 1], side = SparseArray[{}, n - 2, -0.5]}, SparseArray[{i_, i_} :> main[[i]], n - 1] + Sum[side ~DiagonalMatrix~ i, {i, {-1, 1}}] ] This uses several tricks and observations. Credit for the first ...


9

The determinant computation is a matter of memory use in terms of how much we want to store for subdeterminants of a Laplace expansion. Mathematica simply refuses to go that route after 11x11. YOu can do your own as below. myDet[mat_] /; Length[mat] <= 4 := Det[mat] myDet[mat_] := myDet[mat] = Sum[mat[[1, j]]*myDet[Drop[mat, {1}, {j}]], {j, ...


9

In addition to the answers given (ArrayFlatten), there may be cases when the SparseArray-s need to be accumulated one-by-one, and keeping them all in memory at the same time may be too expensive. I've written a generic function to gradually accumulate SparseArray-s. Here is the code: This is the low-level API for SparseArray construction / deconstruction, ...


8

Given the millions of times that code will be run, for a size range of about 1000, all I could think to speed up @Mr.Wizard 's code is to memoize what stays fixed i : diags[n_] := i = With[{side = SparseArray[{}, n - 2, -0.5]}, Sum[side~DiagonalMatrix~i, {i, {-1, 1}}]]; banded2[n_Integer?EvenQ] := With[{main = RandomReal[99, n - 1]}, ...


8

The message tells you what`s wrong: SparseArray::dims: The dimensions 11.` in ...are not given as a list of positive machine integers. You should use something like Round[n+1] as last part. Your comment Isn't n +1 an integer though? 4/.2 + 1 = 21? No. Mathematica makes a distinction between exact Integers and numeric values. In your formula, the ...


8

This is a bug in Pick caused by SparseArray, has nothing to do with Graph. Minimal example (SparseArray object is the fullform version of your vLM): x = {1, 2, 3, 4, 5, 6}; Pick[x, SparseArray[Automatic, {6}, 0, {1, {{0, 3}, {{2}, {3}, {4}}}, {1, 1, 1}}], 0]; FullForm@x {1, System`Private`InternSequence[], System`Private`InternSequence[], ...


7

This is not an answer, but may be it is :) as I do not have time to fully understand the question, just picked up few terms, but just in case, I thought I mention this. Mathematica 8 already has Krylov method in LinearSolve ! so, if you are just looking to use these methods to solve Ax=b, it is already there. Here is an example from my code (I used these ...


7

If you need a batch update, then the answer is in my comment you linked. If you need element-by-element, then there are two cases: Most of values you update are non-zero (or, generally, not equal to default element). In this case, I believe the answer of @Mr. Wizard is optimal, and you should expect update of a single element to be constant time. Most ...


7

There is actually an undocumented System Option that tells Mathematica to do this automatically. The default behavior: ind = {{3, 1}, {3, 3}, {1, 3}, {2, 1}, {3, 2}, {3, 1}, {3, 2}, {3, 3}, {1, 3}, {3, 1}}; val = {1, 1, 3, 0, 3, 4, 3, 1, 1, 1}; SparseArray[ind -> val] // Grid $ \begin{array}{ccc} 0 & 0 & 3 \\ 0 & 0 & 0 \\ 1 & 3 ...


6

This works with matrix of any size (remove Reverse to get it in other direction) rotate45[m_] := SparseArray@ MapIndexed[ Band[{#2[[1]], Length@m + #2[[1]] - 1}, Automatic, {1, -1}] -> Reverse@# & , m] MatrixForm[rotate45[{{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}]] 0 0 64 0 0 0 32 0 16 0 72 0 8 0 56 0 18 0 28 ...


6

For any number of dimensions ( two versions): rot[m_] := Transpose@SparseArray[ Flatten@Map[Band[#] -> Transpose[m][[#[[1]]]] &, Permutations /@ IntegerPartitions[Length@m + 1, {2}], {2}]] or (keeping it short) rot[m_]:= With[{t = Transpose}, t[SparseArray[Band@#-> t[m][[#[[1]]]]&/@ t@{#, ...


6

When you are flattening the expression, you are asking Mathematica to reserve memory for at least Total@Cases[EqL2, SparseArray[x_, y_, z__] :> Times @@ y] (*322850400 elements *) To be more precise, you are only filling Length@Select[Flatten@Cases[EqL2, s_SparseArray :> ArrayRules@s] /. Rule ...


6

Since the sum goes over the last index of $e$ and the first index of $A$, it is directly done by using Dot: dim = 5; e = Array[\[ScriptE], Table[dim, {dim}]]; a = Array[\[ScriptA], Table[dim, {2}]]; c = e.a; Here I defined the arrays with the appropriate dimensions but suppressed the output because it's too long for five dimensions. Another ...


6

You can reduce a lot of the computations by exploiting the symmetry in the problem. Observe the following example: Notice that the 4D matrix is actually a Toeplitz matrix where each element is a 2D matrix, which themselves are Toeplitz matrices. So you really need to only compute the first row in the top line of 2D matrices (here, 9 elements of total 81) ...


6

I believe that Part works quite well. sa = SparseArray[RandomInteger[{1, 10000}, {5000, 2}] -> 1]; new = RandomInteger[{1, 7000}, {5000, 3}]; (sa[[#, #2]] = #3) & @@@ new; // Timing {0.078, Null} Notice that this is being done one element at a time with @@@ and it is still very fast.


6

Looking at the InputForm or FullForm of the expression which, while not equivalent to the internal data format, shows something of the structure and what is stored: SparseArray[{{1, 1} -> 999, {5, 100} -> 999}] // InputForm SparseArray[Automatic, {5, 100}, 0, {1, {{0, 1, 1, 1, 1, 2}, {{1}, {100}}}, {999, 999}}] versus: SparseArray[{{1, 1} -> ...


6

When working with SparseArray objects you should generally try to use functions that operate on SparseArray objects without converting to normal form. You can use UnitStep for threshold operations: row = Range[-5, 5]; r = 2; 1 - UnitStep[r - Abs@row] {1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1} For position you can use reconversion with SparseArray and sparse ...


6

If the background value is 0 just run SparseArray on it: sp = SparseArray[Band[{1, 1}] -> 1, {100, 100}]; ByteCount[sp] (* 2152 *) nrm = Normal[sp]; ByteCount[nrm] (* 40168 *) sp2 = SparseArray[nrm]; ByteCount[sp2] (* 2152 *) If it has another background value do SparseArray[nrm, Automatic, background], if you don't know the background value use the ...


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 ...


6

Take a look at Unitize, UnitStep, Clip, Sign, etc. and the NonzeroPositions property of sparse arrays. Using these in combination with appropriate arithmetic operations you can quickly get positions. You could also take a look at FilterRules, and use this against the array rules to get positions for specified elements. With your updated post, e.g., ...



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