13

No, this is not built-in and I have been waiting quite long for such a feature. PardisoLink With the help of Szabolcs's package "LTemplate`" I had written such an interface for the Intel MKL Pardiso that is shipped with Mathematica a couple of years ago. I spent all day to bring it into a shape that has at least a small chance to run on a computer ...


11

Try result = With[{b = Transpose[a]}, Internal`PartitionRagged[ Transpose[{Flatten[b["ColumnIndices"]], b["NonzeroValues"]}], Differences[b["RowPointers"]] ] ]; // AbsoluteTiming // First 0.074976 This is only difference from the second code snippet that you posted: Internal`PartitionRagged is performed only once and in the ...


11

I can't speak to why exactly it doesn't work, but as for a workaround... If you want to simply count how many values are unspecified, you can take the product of the dimensions (to get the hypothetical number of elements) and then subtract off the number of specifications (note that one specification is the default one, so we must subtract 1): (* s is a ...


10

There are a couple of things that can be improved: Binary search can simply be replaced by a lookup table that is very cheap to build. Moreover, IntegerPart[i/len] leads to rational numbers (so no machine numbers), while Quotient can to the thing in vectorized way. Avoiding the use of a list of rules allows one to work with packed arrays. Using Pick instead ...


9

Using an actual SparseArray and machine precision this evaluates almost immediately with negligible memory footprint. s = 1; n = 10; b1 = N@Table[s - n, {n, 0, 2 s}]; σx = SparseArray@DiagonalMatrix[b1]; σI = IdentityMatrix[2 s + 1, SparseArray, WorkingPrecision -> MachinePrecision]; σxx[j_] := KroneckerProduct[## & @@ ConstantArray[σI, j - 1], σx, ## ...


8

For a start, I think this should provide the correct result. Do you confirm that? cf = Compile[{{a, _Integer, 1}, {i, _Integer}}, Transpose[{a + 1, 1 + BitXor[a, 2^(i - 1)]}], CompilationTarget -> "C", RuntimeAttributes -> {Listable}, Parallelization -> True ]; ClearAll[A]; A[n_] := With[{a = Range[0, 2^n - 1]}, SparseArray[...


7

Maybe with SymmetrizedArray? size = 4; rank = 4; Av = <|{1, 4, 2, 3} -> a, {2, 4, 2, 4} -> b|>; symmetries = { {{4, 2, 3, 1}, 1}, {{4, 3, 2, 1}, 1}, {{1, 3, 2, 4}, 1}, {{2, 1, 4, 3}, 1}, {{3, 1, 4, 2}, 1}, {{3, 4, 1, 2}, 1}, {{2, 4, 1, 3}, 1} }; A = SparseArray[ SymmetrizedArray[Normal[Av], ConstantArray[n, rank], ...


7

It appears that the proximity of the eigenvalues causes Eigensystem with the default parameters to be inaccurate. This can be fixed by increasing the basis size to 30 Import["https://pastebin.com/raw/PpDfY3EQ", "Package"]; mysparsemat = mymat; mymat = Normal[mysparsemat]; m = 2; Reverse[First[Eigensystem[mymat, -m]]] Reverse[First[Eigensystem[...


7

saCount[s_SparseArray, a_] := Block[{v = s["NonzeroValues"] }, Count[v, a] + If[a == s["Background"], Times @@ Dimensions[s] - Length@v, 0]] saCount[a, 0] 10 Timings: sa = SparseArray[Table[{2^i, 3^i + i, i^5} -> 1, {i, 10}]] saCount[sa, 0] // RepeatedTiming {6.5*10^-6, 6047641599990} SparseCount[sa, 0] // RepeatedTiming (* from thorimur's ...


5

This seemed to work for me... test1 = DiagonalMatrix[SparseArray[{1. + 0. I, 0. I}]]; test2 = DiagonalMatrix[SparseArray[{0. I, 1. I}]]; test1.test2


5

You seem to do something wrong because the LIL you provide is more suitable to assemble the transpose of the desired matrix in CRS format (or to assemble the desired matrix in CCS format). Since Mathematica uses CRS, I show you how to assemble the transpose. First two compiled helper functions: getColumnIndices = Compile[{{p, _Integer, 1}, {a, _Integer, 2}}, ...


5

pg = PermutationGroup[{{1, 2, 3, 4}, {4, 2, 3, 1}, {4, 3, 2, 1}, {1, 3, 2, 4}, {2, 1, 4, 3}, {3, 1, 4, 2}, {3, 4, 1, 2}, {2, 4, 1, 3}}]; ClearAll[sAbuild] sAbuild = SparseArray[KeyValueMap[Alternatives @@ GroupOrbits[pg, {#}, Permute][[1]] -> #2 &]@#, #2] &; dims = {4, 4, 4, 4}; sAbuild[Av, dims] Based on Henrik's answer, we ...


4

Because of the banded structure of your matrix, you may use the undocumented function SparseArray`SparseBlockMatrix for that as follows (using a dummy matrix-valued function P here): P[i_, l_, n_] := ConstantArray[l, {n, n}]; R[k_, l_, n_] := SparseArray`SparseBlockMatrix[ Table[Band[{i, 1}] -> 1/i! P[i, l, n], {i, 1, k}], {k, k} ] E.g., R[4, ell, ...


4

Not an answer but too long for a comment. Well, this additional memory consumption is actually not as bad as you might think, and I am not so sure whether this should be considered a memory leak. The additive assembler has to sort the entries of Transpose[e[[1;;2]]]; to reorder the entries of e[[3]]; to compute the arrays for the row pointers; to compute ...


4

Using N[...] didn't work for me either. If you can tolerate the really tiny error on the order of $10^{-308}$ then here's a workaround which adds the $MinMachineNumber to the first matrix elements: test1 = SparseArray[DiagonalMatrix[{1., 0} + $MinMachineNumber]] test2 = SparseArray[DiagonalMatrix[{0, I}]] test1.test2


4

While comparing the results of MATLAB and Mathematica for a little experiment the other day, I got bitten by an error, which in hindsight was because I did not pay attention to this part of the help file for MATLAB's sparse(): sparse adds together elements in v that have duplicate subscripts in i and j. As a demonstration, here's a small MATLAB example: ...


3

I have also encountered this issue and it occurs also for small Sparse matrices. In order to identify whether the eigenvalues supplied by the Arnoldi algorithm are degenerate or not I deform the matrix by adding to it a diagonal matrix with random elements that are about 5-6 orders of magnitude less than the original matrix. This usually slightly lifts the ...


3

Assuming you have definitions already for n, single argument t[x], and two-argument t[x,y], then this is fairly straightforward: g[i_, n_] := Table[(t[i + 1] - t[j, i])/h[i] KroneckerDelta[k, i] + (t[j, i] - t[i])/h[i] KroneckerDelta[k, i + 1] - 1/6 (t[j, i] - t[i]) (t[i + 1] - t[j, i]) (1 + (t[i + 1] - t[j, i])/h[i]) KroneckerDelta[k, ...


3

As Carl pointed out, RuleDelayed :> is your friend in this case and fixes the issue - MatrixForm[SparseArray[{{jj_, kk_} /; EvenQ[IDX[degree][[jj]][[1]] + IDX[degree][[kk]][[1]]] && EvenQ[IDX[degree][[jj]][[2]] + IDX[degree][[kk]][[2]]] && EvenQ[IDX[degree][[jj]][[3]] + IDX[degree][[kk]][[3]]] :> ...


3

Something like this should work. dims = Prepend[(Dimensions /@ bndrs)[[All, 2]], Dimensions[bndrs[[1]]][[1]]]; file = OpenWrite["a.txt"]; WriteString[file, ExportString[{dims}, "Table"]]; Do[ WriteString[file, "\n\n"]; WriteString[ file, ExportString[ Join[A["NonzeroPositions"], Partition[A["...


3

This seems to work, and returns an empty SparseArray as desired test1 = SparseArray[DiagonalMatrix[SetPrecision[{1., 0}, $MachinePrecision]]] test2 = SparseArray[DiagonalMatrix[{0, I}]] test1.test2


3

This method is short, but slow at higher resolutions: ImageApply[RandomVariate@*BernoulliDistribution, ImageResize[img, {100, 100}]] Instead, it makes more sense to pre-generate a random image first which is much faster at high resolutions: With[{dims = {1920, 1440}}, Binarize[ImageSubtract[ImageResize[img, dims], RandomImage[1, dims]],0]] See also my ...


3

Maybe using RandomChoice works better (it does so for me). f[img_, k_] := Image[ArrayFlatten[ Map[ RandomChoice[{1 - #, #} -> {0, 1}, {k, k}] &, ImageData[img], {2} ] ]]; Example: f[ExampleData[{"Texture", "Bark"}], 5]


2

Basically the following should do (replacing N by n and C by c): SparseArray[ Rule[ Transpose[{ Join @@ KeyValueMap[ConstantArray[#1, Length[#2]] &, n], Join @@ Values[n] }], Join @@ Values[c] ] ] To problem is that your numbers are too large. The matrix has as many rows as the largest key in the association n. And a sparse array has ...


2

First we need same data. I denote your "P(l)" bl p[l] and `P^(1)" by der[n,p,x]. Then I define a function "makeblmat" that assembles the block matrix: m = Table[RandomInteger[{-10, 10}], 2, 2]; p[x_] = Sum[MatrixPower[m, i] x^i, {i, 0, 2}]; derp[n_, p_, x_] := D[p, {x, n}]; makeblmat[k_] := Table[If[i >= j, derp[i - j, p[x], x]/!...


2

Use Normal to make the RHS of the definition of F2 a normal array. The formal arguments were being hidden in the SparseArray: 0[x_, y_] = (3 - (Cos[x] + Cos[y])) PauliMatrix[0] + 5 PauliMatrix[3]; nf = 2; F1[x_, y_] = SparseArray[{Band[{1, 1}, {2 nf, 2 nf}] -> {f0[x, y]}, Band[{1, 3}, {2 nf, 2 nf}] -> {7 PauliMatrix[0]}, Band[{3, 1}, {2 ...


2

I am not sure if I have got your attention properly, but you can see if code below works for you or not, matM = Module[{block, n = 4, jup = 10}, block[i_, j_] := Which[i == j, Cos[i], Abs[i - j] == 1, Sin[Min[i, j]], True, 0 ] IdentityMatrix[n]; SparseArray @ ArrayFlatten[Table[block[i, j], {i, (*0,*) jup}, {j, (*0,*) jup}]] ]


2

Is this a formula with the Einstein sum convention. If so then the summation over two same indices has to be carried through. g[i_, j_, k_, n_] := Sum[(t[i + 1] - t[j, i])/h[ii] KroneckerDelta[k, ii] + (t[j, i] - t[ii])/h[ii] KroneckerDelta[k, ii + 1] - 1/6 (t[j, i] - t[ii]) (t[ii + 1] - t[j, i]) (1 + (t[ii + 1] - t[j, i])/h[ii]) ...


2

Spectral clustering might be a good candidate here. Generally, spectral clustering works as following: Find a few largest eigenvectors of the adjacency matrix by magnitude, let's say we choose largest M vectors. Treat each vertex as a N dimensional one-hot unit vector (where N is the number of vertices). Project each vertex into M dimensional "feature&...


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