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}$


15

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


15

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


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

ArrayFlatten[{{sa11, sa12}, {sa21, sa22}}] seems to be what you need. It automatically merges everything into one big SparseArray[].


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


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


12

Yes, it is possible! There is a WorframGPULibrary (WGL), which I discover recently. It is undocumented, however there are beautiful examples in $InstallationDirectory/SystemFiles/Links/CUDALink/CSource/ It is similar to LibraryLink, but allows CUDAMemory as an argument. I wrote the code below to call main CUSPARSE routines directly from Mathematica $$ C ...


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


10

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


10

How about ArrayFlatten (docs): sa2 = ArrayFlatten[{{sa11, sa12}, {sa21, sa22}}] sa==sa2 (* True *)


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


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

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

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


8

This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"] Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"] Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = ...


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[], ...


8

Reposting my answer from here (its relevant part about SparseArray) The anatomy of sparse arrays We start with a generally useful API for construction and deconstruction of SparseArray objects: ClearAll[spart, getIC, getJR, getSparseData, getDefaultElement, makeSparseArray]; HoldPattern[spart[SparseArray[s___], p_]] := {s}[[p]]; getIC[s_SparseArray] := ...


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

mat = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; With[{j = Length[mat] - 1}, Table[ArrayPad[Riffle[Diagonal[mat, k], 0], Abs[k]], {k, j, -j, -1}]]


7

Everything needs to be of the same precision including the background element. Then this: sp = SparseArray[{{2, 2} -> 1.}, {50, 50}, 0.]; ArrayFlatten[{{sp, 0.}, {0., -sp}}] will return a SparseArray without creating a dense matrix first.


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


7

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


7

UPDATE The bug is fixed in MMA 10.0 (see original question for details) 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 ...


7

The basic reason is that once you convert a tensor expression into a SparseArray, you've "given control" of all levels of that expression to SparseArray to manage on your behalf in an efficient way (the number of levels is the rank of the tensor, to mix jargon). SparseArray will then try to maintain the illusion that those levels are still really there. ...


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



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