Hot answers tagged

13

The fastest way to get the identity matrix as a sparse array is simply this: IdentityMatrix[10000, SparseArray]; // AbsoluteTiming Here are just some thoughts on why SparseArray is not the default for IdentityMatrix: You don't always want SparseArray output when defining matrices, and it's not always possible to decide automatically whether a SparseArray ...


9

The reason for that is that MN is numerical, since you have 0.5 in it. So in the Do loop, every step is done just numerically. In refl1 the MatrixPower is analytically performed and converted to numerical just as last step. If you change the MatrixPower input also to a numerical input the execution times is much faster with the MatrixPower function. refl3[...


7

How about this: a = ConstantArray[1, {4, 4}] (* ==> {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}} *) b = ConstantArray[0, Dimensions[a]] (* ==> {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} *)


7

Three possibilities: With[{n = 4}, ArrayFlatten[{{NestList[RotateRight, Array[B, n], n - 1], 1}, {{Array[A, n]}, 1}}]] With[{n = 4}, ArrayFlatten[{{ToeplitzMatrix[RotateRight[Reverse[Array[B, n]]], Array[B, n]], 1}, {{Array[A, n]}, 1}}]] With[{n = 4}, PadRight[Append[NestList[RotateRight, Array[B, n], ...


6

If your matrix has no expressions in level -1, this could work for any matrix. a={{1,2,{3}},{5,6,{7,{8,9}}}} b=Replace[a,_->0,{-1}] (*{{0,0,{0}},{0,0,{0,{0,0}}}}*) You could also do simple multiplication by 0 a*0 (*{{0,0,{0}},{0,0,{0,{0,0}}}}*)


6

SparseArray objects are somewhat special and some of the access operations can be slower with them. IdentityMatrix though does return a more compact object than just a list of lists -- it returns a packed array. Needs["Developer`"] Tally[ Table[PackedArrayQ[IdentityMatrix[n]], {n, Range[1, 1000, 10]}]] (* {{True, 100}}*) Also, it is really easy to ...


5

You need PseudoInverse: mat = {{0, 1, 1}, {0, 2, 4}, {0, 3, 9}}; PseudoInverse[mat].{{45}, {130}, {255}} {{0}, {25}, {20}} or, LeastSquares (thanks: J.M.) LeastSquares[mat, {{45}, {130}, {255}}] {{0}, {25}, {20}}


5

Here are a few ways, each returning <| {1} -> "a", {3} -> "b" |>: Association[{#} -> #2 & @@@ test] GroupBy[test, Most -> Last, First] ReplacePart[test, {0 -> Association, {_, 0} -> ({#} -> #2 &)}] test // Query[Association, Most@# -> Last@# &] test // Query[GroupBy[Most -> Last], First]


5

Collecting the comments as an answer for the reader. This was my first thought: AssociationThread @@ Transpose @ test <|1 -> "a", 3 -> "b"|> But an undocumented but somewhat slower function also came to mind: GeneralUtilities`AssociatePairs[test] <|1 -> "a", 3 -> "b"|> Performance testlist = RandomInteger[100, {...


5

Iterating @J.M.'s comment: This problem has no exact solution. With[{ matrix = {{0.8111, 0.4867, -0.3244}, {a, b, 0}, {c, d, e}} }, Print[matrix.Transpose[matrix]]; Solve[ matrix.Transpose[matrix] == IdentityMatrix[3], {a, b, c, d, e} ] ] (* {{0.999995,... *) (* {} *) That is, the first column's first entry is not $1$, so there is ...


5

Here's one way: ap = ConstantArray[0.001, {5, 10}]; b = {0.001, 0.005, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1, 0.5, 1}; z = {{1, 3}, {2, 2}, {5, 7}}; f[ap_, {z1_, z2_}] := ReplacePart[ap, {z1, i_ /; i >= z2} :> b[[i]]] Fold[f, ap, z] (* {{0.001, 0.001, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1, 0.5, 1}, {0.001, 0.005, 0.009, 0.01, 0.03, 0.05, 0.082, 0.1,...


4

Here is an expanded version of JM's implementation of your method that he proposed in comments. As an aside, I found this interesting write-up by Michele Benzi (Emory University) on Cimmino, his method and other accomplishments, and the Italian school of numerical analysis in the 1920s-30s; a very interesting read. I first propose a version using the ...


4

I still will post this answer though, fortunately, OP has already got a solution~ The following code will work: Tally[Flatten[Subsets[Sort[#], {4}] & /@ A, 1]] Tally[Flatten[#~Subsets~{4}&/@A,1],Sort@#==Sort@#2&] Truely an interesting thing for @J.M. and I came up woth the same solution ay the same time. :) cheers~


4

ClearAll[f] f = With[{a = #, s = #2, n = #3}, Commonest[Flatten[Subsets[Sort@#, {s}] & /@ a, 1], n]] &; f[A, 4, 1] {{2, 21, 49, 53}} f[A, 4, 2] {{2, 21, 49, 53}, {31, 35, 44, 46}} f[A, 3, 2] {{2, 21, 49}, {2, 21, 53}} f[A, 5, 2] {{2, 3, 21, 49, 53}, {26, 31, 35, 44, 46}}


3

Customized function for your demand. SeletSubTuple[l_,n_]:=Keys[TakeLargest[Counts[Sort/@Catenate[Subsets[#,{4}]&/@l]],n]] Usage SeletSubTuple[A, 1] {{2, 21, 49, 53}} SeletSubTuple[A, 2] {{2, 21, 49, 53}, {31, 35, 44, 46}} If this can serve,It's my honor. :)


3

It is because you are using exact numbers. Use floating number instead. Like Timing[Do[refl1[100., 2., 3., 5., 6., 100.], {100}]] Timing[Do[refl2[100., 2., 3., 5., 6., 100.], {100}]] {0.012001, Null} {0.016001, Null} which is the counter example of your counter intuition :)


3

headF1 gets the Head of the expression in the specified part of the matrix. headF2 uses the fact that Part 0 of an expression is its Head. ClearAll[headF1,headF2] headF1= Head[#[[## & @@ #2]]] &; headF2= #[[## & @@ #2]][[0]] &; SeedRandom[1] mat = RandomChoice[{Ellipse[], Point[]}, {5, 3}] {{Point[], Point[], Ellipse[]}, {Point[...


3

Your example can be achieved using Map with a level specification, Partition to generate the sub-matrices and Tr to calculate the traces. ClearAll[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] r = {{a, b, c, d}, {e, f, g, h}, {i, j, k, l}, {m, n, o, p}}; Map[Tr, Partition[r, {2, 2}], {2}] {{a + f, c + h}, {i + n, k + p}}


2

SeedRandom[5] pts = RandomReal[1, {6, 2}]; pts = pts[[FindShortestTour[pts][[2]]]]; am = RandomChoice[{.7, .3} -> {0, 1}, {6, 6}]; AdjacencyGraph using the polygon vertices as vertex coordinates: Labeled[AdjacencyGraph[am, VertexCoordinates -> pts, DirectedEdges -> False, Vertexlabels->"Name", Prolog -> {Yellow, ...


2

For reference, here is how to generate a modular $\mathbf L\mathbf D\mathbf U$ decomposition, as suggested by Daniel in the comments: mat = {{1, 0, 2}, {2, 1, 3}, {2, 1, 2}}; m = 5; {lu, piv, cond} = LUDecomposition[mat, Modulus -> m]; l = LowerTriangularize[lu, -1] + IdentityMatrix[Length[lu]]; d = DiagonalMatrix[Diagonal[lu]]; u = Mod[DiagonalMatrix[...


2

If you make your A matrix a function (but avoid starting with capital letters), like aMatrix[x_] := ... then you can use Nest: Nest[aMatrix, x0, 100] where x0 is the starting vector.


2

mA = {{1, 2}, {3, 4}}; mC = {{19, 22}, {43, 50}}; mX = Array[X, {2, 2}] Solve[mA.mX == mC, Flatten[mX]] Will do what you want


2

list = RandomReal[{0, 11}, {3, 3}]; list = list + 5.5 - Mean[Mean[list]] Mean[Mean[list]] {{8.82553, 10.7335, 4.63438}, {2.8386, 1.73321, 8.4593}, {1.64362, 1.59518, 9.03668}} 5.5


2

Use Part. E.g. to exchange row 2 and 3 while also exchanging column 1 and 2 in a 3x3 matrix, rowOrd = {1,3,2}; colOrd = {2,1,3}; mat[[rowOrd, colOrd]]


2

This is just play. In the following the list A in OP is a: Finding the pairwise non-empty intersections and counting: tal = (Tally[ Flatten[Outer[Intersection, a, a, 1] /. {} -> Sequence[], 1]] /. {__, 1} :> Sequence[])[[All, 1]]; Determining desired result: u = tal~Join~a; rg = RelationGraph[SubsetQ[#1, #2] && MemberQ[a, #1] ...


2

The most I've managed to improve the speed is about a factor of 2, but I thought I would share my attempt anyhow. First, let's just compute the identity matrix once, instead of once each iteration step: id = SparseArray[IdentityMatrix[2n]]; Second, since we are going to need all the powers of both A1 and A2, we can gain some speed by simply bumping the ...


2

I am not sure I understand. Here are some ways to group by last element: list={{1, 1}, {1, 3}, {2, 1}, {2, 2}, {2, 3}} GatherBy[list, Last] GroupBy[list, Last] Last@Reap[Sow[{##}, #2] & @@@ list, _, Rule] yielding respectively: {{{1, 1}, {2, 1}}, {{1, 3}, {2, 3}}, {{2, 2}}} <|1 -> {{1, 1}, {2, 1}}, 3 -> {{1, 3}, {2, 3}}, 2 -> {{2, 2}}|&...


2

I think I know what you need: SortBy[GatherBy[list,Last],Last@*Last][[;;,;;,1]]/.{x_?AtomQ}->x The result is: {{a,c},d,{b,e}} This will gather the elements by the last member of each small list, sort it by the index number and then throw away all the index numbers. Finally, it can change all one element list into the number itself. Will this help ...


2

If we assume that the symbol x is reserved for the matrices you're planning to use, and the individual matrices are named x[i], then you can do this: xProduct[indices__] := Signature[{indices}] Dot @@ x /@ {indices} xPerm[a_List] := Total[xProduct @@@ Permutations[a]] xPerm[{3, 4, 5}] (* ==> x[3].x[4].x[5] - x[3].x[5].x[4] - x[4].x[3].x[5] + x[4]....


2

a = Table[j^2, {j, 1, 25}]; b = Table[Log@j, {j, 1, 25}]; ListPlot[Transpose[{a, b}]]



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