Tag Info

19

Introduction This post is long overdue as I have been repeatedly asked to explain code of mine containing these things. As I see increased use of this construct by others perhaps it is past due also. SparseArray objects can behave as functions accepting certain arguments to return internal data or efficiently return data in certain forms. These are known ...

18

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

17

I found a way to dramatically improve the performance of this algorithm by using the undocumented function SparseArrayKrylovLinearSolve. 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

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

14

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

14

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 ... 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[]. 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 ... 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 *) 10 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. ... 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 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 ... 8 The message tells you whats 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 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}]] 8 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. 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, SystemPrivateInternSequence[], SystemPrivateInternSequence[], ... 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] := ... 8 Update: here Table is faster and more user-friendly then Array. mat[n_] := LowerTriangularize@Table[2 (1 + Boole[j > 1]) (i - 1) Mod[i + j, 2], {i, n}, {j, n}]; mat[10] // MatrixForm It is fast and the result is packed array mat[1000] // DeveloperPackedArrayQ // AbsoluteTiming (* {0.142522, True} *) 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 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 ...

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