New answers tagged

1

The solution to your Eigenvalues function are Root objects, see the documentation. Root[f,k] represents the exact k^(th) root of the polynomial equation f[x]==0. Inside Root is the pure function in the argument #1. If we named this pure function f with argument x, this would correspond to the situation in the above quote with f[x]. Your Matrix $A$ ...


4

Another way to go is using NMinimize NMinimize[Min@Abs@Eigenvalues[{{a, 2}, {3, 4}}], a] (* {3.89083*10^-10, {a -> 1.5}} *) which of course matches the analytic answer above


4

To get the expresions for the Eigenvalues Eigenvalues[{{a, 2} , {3, 4}}] {1/2 (4 + a - Sqrt[40 - 8 a + a^2]), 1/2 (4 + a + Sqrt[40 - 8 a + a^2])} make each expresion Equal to zero (f@x is the Prefix form of f[x]) Thread@Equal[Eigenvalues[{{a, 2} , {3, 4}}], 0] {1/2 (4 + a - Sqrt[40 - 8 a + a^2]) == 0, 1/2 (4 + a + Sqrt[40 - 8 a + a^2]) == 0} ...


5

The problem is that Dot[a,b] (a and b being atomic, e.g. symbols with no values) evaluates differently than Dot[{a,b},{x,y}] (i.e. the arguments being lists). Dot[{a, b}, x] does not evaluate, so you can transform it using Thread. Dot[{a, b}, {x, y}] does evaluate before it even sees Thread. Dot[{a, b}, {x, y, z}] tries to evaluate and gives up with an ...


8

After some investigation I can mostly explain the (correct) behavior. First off, we set this up using machine doubles. After computing the eigensystems we reorder by eigenvalues. While there is the possibility of messing up conjugate pairs (wherein right and left eigenvalues get ordered differently due to small numeric discrepancies), I checked and this ...


5

Here is a solution without the mess of reindexation : NN = 5; c[i_] = If[i == 0 || i == NN, 2, 1]; x[j_] = Cos[j Pi/NN]; result = Table[Switch[{i, j}, {0, 0}, (2 NN^2 + 1)/6, {NN, NN}, -(2 NN^2 + 1)/6, {xy_, xy_}, -x[j] / (2 (1 - x[j]^2)), {_, _}, c[i] (-1)^(i + j)/(c[j] (x[i] - x[j]))], {i, 0, NN}, {j, 0, NN} ]; result // ...


2

Use the third argument to IntegerDigits to specify the length of the digit sequence required, padded on the left by zero. Thus, reconstructArray = ArrayReshape[IntegerDigits[code, 2, 8*7], {8, 7}]


4

Alternatively to MarcoB's solution (but using their definitions), use a Table and a Which or a Piecewise. This is strictly for the purpose of showing more methods. matrixgenerator[n_Integer] := Module[{x, c}, x[i_] := Cos[i Pi/n]; c[i_] := If[i == 0 || i == n, 2, 1]; Table[ Which[ i == j == 0, (2 n^2 + 1)/6, i == j == n, -(2 n^2 + 1)/6, ...


5

(Update: fixed incorrectly translated rules) Remember that array indices start at 1 in Mathematica, so you have to adapt your definitions accordingly. Perhaps you could have started there... You can quite literally translate your requirements into conditions and feed them to SparseArray, then use Normal to get a standard representation: ...


4

Make j as the function argument for the matrix. mat[j_] := {{2 j, j, -j, 0}, {2, j, -j, -2}, {2, 3, j, -j}, {0, j, -j, 2}}; Then Manipulate works Manipulate[ mat[j] // MatrixForm, {j, 0, 10, ControlType -> Animator, AnimationRate -> 1, RefreshRate -> 10}]


4

m = {{2, 5, 1, 0}, {2, 0, -1, -2}, {2, 3, 4, -3}, {0, 1, -2, 2}}; base[m_] := Transpose[SortBy[Eigenvectors[m] // N, Norm]] diag[m_] := Chop[Inverse@#.m.#]&@base[m] diag[m] // MatrixForm


3

For the data in the question, the following also works. ListPlot[Transpose[Split[data, First[#1] === First[#2] &]], PlotStyle -> {Blue, Green, Purple, Orange, Red}] This, of course, assumes that data already is ordered by size. If not, use ListPlot[SortBy[Transpose[Split[data, First[#1] === First[#2] &]], Last], PlotStyle -> ...


3

How about this: n=5; dt = Table[Select[SortBy[data, Last], #[[1]] == j &], {j, 1, n}]; ListPlot[Table[dt[[1 ;; n, eid]], {eid, 1, 5}], PlotStyle -> {Blue, Green, Purple, Orange, Red}]


5

tf[nn_] := With[{n = Range[0, nn]}, ToeplitzMatrix[(-1)^n n x, -(-1)^n n x]] For the question in the comment: tf2[nn_] := With[{n = Range[0, nn]}, Module[{expr = (-1)^n Cot@(n x)}, expr[[1]] = 0; ToeplitzMatrix[expr, -expr]]] tf3[nn_] := With[{n = Range@nn}, With[{expr = Join[{0}, (-1)^n Cot@(n x)]}, ToeplitzMatrix[expr, -expr]]]


1

Try this one. flip[a_, n_Integer] := Module[{pos} , pos = Union[#, Reverse /@ #] &@RandomSample[SparseArray[a]["NonzeroPositions"], n] ; ReplacePart[a, Thread[pos -> -2 + Extract[a, pos]]] ] It also makes the matrix symmetric. flip[Array[b, {3, 3}], 2] // MatrixForm You can put back RandomChoice in place of RandomSample if you want.


1

Simple syntactic errors. This works: flip[a_List , n_] := Block[{lis1, lis2, s, size}, size = Dimensions[a]; lis1 = Most[ArrayRules[a]]; lis2 = RandomChoice[lis1, n] ; lis2 = Map[ReplacePart[#, 2 -> -2] &, lis2[[1 ;; n]]]; s = SparseArray[lis2, {size[[1]], size[[1]]}, 0] + a] Don't use upper-case letters for variables or functions as they may ...


2

In general, when you see a function prototype with a formal argument of the form oneflag_ : 0, it means the actual argument can be of any type (or more precisely have any head), but if it is omitted, then the value 0 will be used. Look up Optional in the documentation.


2

I have previously used the following two helper functions to generate the format of covariance and correlation matrices: covariancematrix[n_] := Table[ σ[i] σ[j] ρ[i, j]^(1 - KroneckerDelta[i, j]), {i, 1, n, 1}, {j, 1, n, 1} ] /. {ρ[i_, j_] :> ρ[j, i] /; i > j} correlationmatrix[n_] := Table[ σ[i] σ[j] ρ[i, j]^(1 - KroneckerDelta[i, ...


4

The formula for spectral norm you are using is meant to be the formal mathematical definition of the quantity. However this is restrictive for practical use as symbolic norm calculation on high dimensions are very cumbersome. The formulation you might be looking for is the following. Here $\mu_{2}$ is the logarithmic two norm. $$\mu_{2}(A) := ...


4

Algebra approach. Dot Wwith a column vector of 1's. W . ConstantArray[1, {Last@Dimensions@W, 1}] Was just curious if Dot approach was faster than Total/@ for large symbolic matrices after reading comments. sqSymMx[m_Symbol, n_Integer?Positive] := Table[Indexed[m, {i, j}], {i, n}, {j, n}]; t = With[{r = sqSymMx[x, #]}, {#, First /@ ...


5

List@*Total /@ W (V10 only) List /@ Total /@ W (V10 or earlier) (* {{Subscript[x, 11] + Subscript[x, 12] + Subscript[x, 13]}, {Subscript[x, 21] + Subscript[x, 22] + Subscript[x, 23]}, {Subscript[x, 31] + Subscript[x, 32] + Subscript[x, 33]}} *) other alternatives: List /@ Plus @@@ W List /@ Total[W, {2}]


7

List@*Total /@ W % === g $\ $ True


3

"Monster voodoo machine" method for those who want MapAt, do not want to bother with Part and like Span: MapAt[Replace[{a_, b_, c_, d_} -> {c, b, a, d}], m1, {;; , ;;}]


5

m3 = m1 /. ({#, #2, #3, _} -> {##} & @@@ Flatten[m2, 1]) { {{1, 2, 2, 3}, {1, 1, -2, 1}, {3, 2, 2, -I}, {1, 2, 0, -I}}, {{3, 4, 4, 0}, {1, 1, 4, 1}, {3, 3, 4, -1}, {1, 1, 2, I}} } So basically we create replacement rules from m2, e.g. from {1,0,2,1} we get {1,0,2,_} -> {1,0,2,1}. Then we replace it in m1, if {a,b,c,_} matches, those 3 ...


2

mat = {{209, 64, 112}, {8, 96, 253}, {65, 200, 95}}; Unitize[mat, 95]; This is fast, but not faster than your version: mat = RandomInteger[{1, 300}, {1000, 1000}]; Unitize[mat, 95]; // AbsoluteTiming // First ImageData[Binarize[Image[mat], 94]]; // AbsoluteTiming // First UnitStep[mat - 95]; // AbsoluteTiming // First Boole[Map[# <= 95 &, mat, ...


3

You can also use the Sign function, though you have to replace the -1 with 0 Sign[m - t] /. {-1 -> 0}


4

I think this does what you're asking for. t = 95; M = {{209, 64, 112}, {8, 96, 253}, {65, 200, 95}}; UnitStep[M - t] {{1, 0, 1}, {0, 1, 1}, {0, 1, 1}}


6

One way: m2 = Apply[{#3, #2, #, #4} &, m1, {2}] or another: m2 = m1; m2[[;; , ;; , {3, 1}]] = m2[[;; , ;; , {1, 3}]]; m2


1

Code: DY = {{Cos[ay], 0, Sin[ay]}, {0, 1, 0}, {-Sin[ay], 0, Cos[ay]}}; Output: A potential error: {{Cos[ay], 0, Sin[ay]}, {0, 1, 0}, {-Sin[ay], 0, Cos[ay], 0}}; Look at the sub-list no. 3, you've got additional 0 in there making the lists of un-equal lengths;


3

For example (Partial order here is vertex reachability) SeedRandom@42; << Combinatorica` g = System`RandomGraph[{9, 7}, VertexLabels -> "Name"]; g1 = System`Graph[VertexList@g, DirectedEdge @@@ EdgeList@g]; ShowGraph[ HasseDiagram[ MakeGraph[VertexList[g1], GraphDistance[g1, #1, #2] =!= Infinity &]], VertexNumber -> ...


4

If you want to compare all to all, use Outer. Say, we have a function h with [[Span]]: h = (#1[[1 ;; -2]] == #2[[1 ;; -2]]) & Note, that -2 suits the list of any length longer than 2. Than, with your data structure: Outer[h, m1[[1]], m2[[1]], 1] // MatrixForm We may get positions of True: pos = Position[Outer[h, m1[[1]], m2[[1]], 1], _?TrueQ] (* ...


3

To index into arrays and consider more than a single element, use ;;. Thus you can deal with elements 2 through 7 of an array x with x[[2;;7]] Thus your three && lines can be replaced by If[m1[[1, i]][[1;;3]] == m2[[1, j]][[1;;3]] ... ] Please note that this is all perfectly well documented in the docs for Part.


0

It looks as if this is just a problem in indexing properly. If I understand the question, you can distinguish the indices where the ninth element needs to come from mainmatrix1 and where it needs to come from matrix2: ind = Table[If[mainmatrix1[[i, j, 1 ;; 8]] == matrix2[[i, j, 1 ;; 8]], 1, 0], {i, 1, Length[mainmatrix1]}, {j, 1, ...


4

The dot product (Dot) in Mathematica is defined as that (discrete) inner product for which the first operation is multiplication and the second, addition. So, you cannot use a dot product directly here, because it cannot incorporate concepts of differentiation or functional application. Instead, you should use a suitably constructed generalized inner ...


6

MapThread[ Append, {Outer[Join, a, a, 1], d}, 2 ]


5

Let's build your matrix in three steps. a = {{1, 2, 2, 1}, {3, 4, 4, 3}, {8, 5, 5, 8}}; d = {{I, 2, -I}, {I, 1, -1}, {4, I, 0}}; m1 = Join[#, #] & /@ a {{1, 2, 2, 1, 1, 2, 2, 1}, {3, 4, 4, 3, 3, 4, 4, 3}, {8, 5, 5, 8, 8, 5, 5, 8}} m2 = ConstantArray[#, 3] & /@ m1 {{{1, 2, 2, 1, 1, 2, 2, 1}, {1, 2, 2, 1, 1, 2, 2, 1}, {1, 2, 2, 1, ...


5

Xavier's comment with an additional Partition gives: Partition[Flatten /@ Transpose[{Tuples[a, {2}], Flatten@d}], Length@d] I think this is what the OP had in mind but it doesn't correspond to the next to last row of his "desired result."


1

You can ParallelCombine as well. This has the added benefit of working on a list of more than the iteration limit of the session. My limit is 4096 so I would not be able to dot product more than 4096 matrices without using this or a similar method. matrices = RandomReal[2, {10000, 2, 2}]; ParallelCombine[Dot[Sequence @@ ##] &, matrices, Dot] Hope ...


2

I also voted to close. Thought I'd show anyway how one might go about this as an optimization problem using Mathematica. We set up an example using a symmetric 6x6 matrix and four others we'll use to try to approximate the original. We only are given the eigenvalues of the original. SeedRandom[1111]; mat = RandomReal[{-10, 10}, {6, 6}]; symmat = mat + ...


7

The following is ten times faster. The remaining time is mostly consumed while evaluating your function, so there may be some optimization window there. point1[j_] := Join[x[[;; j]], w[[j + 1 ;;]]]; point2[j_] := Join[x[[;; j - 1]], w[[j ;;]]]; max = 11; fx = f[x]; β = SparseArray[{{i_, i_} -> -1/100}, {size, size}]; Do[{ w = x + β.fx; T = ...


4

General idea A general rule of thumb to reduce the time and memory needed for a computation is to use inexact (floating-point) numerics instead of exact or symbolic approaches, and to use it early. Of course, one might worry about accuracy, but there's no guarantee that feeding an exact, symbolic problem to FindRoot will be more accurate than feeding an ...


1

I like march's idea of automation but I think at the moment his updated code is not working. Perhaps this will serve the purpose: SetAttributes[heldDistribute, HoldFirst] heldDistribute[expr_] := Unevaluated[expr] /. Cases[Unevaluated[expr], x_Symbol :> (HoldPattern[x] :> Defer[x]), {-1}, Heads -> False] // Distribute Now with the ...


0

Maybe with the general Taylor formula you can calculate the Taylor remainder easier. This applies to any vars. taylor = (vars - point).# &; init := D[f[vars], {vars, j}] /. Thread[vars -> point]; taylorPolynom[m_] := Sum[1/j! Nest[taylor, init, j], {j, 0, m}] remInit := D[f[vars], {vars, j}] /. Thread[vars -> point + t (vars - point)] ...


3

Probably: p = {x, y, z}; ff[x_, y_, z_] := (x - y)/(x + y) + z^3 hhh = D[ff[x, y, z], {p, 2}] // Simplify h1 = hhh /. MapThread[Rule, {p, p + t { u1, u2, u3}}] /. Thread[p :> {10, 5, 1}] You can get the same result you got with your code with the following (please note that most of the code is just formatting,not sure why you may want ...


2

mat = {{1, 2}, {3, 4}} ArrayPad[#, {{0, 0}, {#2, #2}} & @@ Dimensions[#], "Periodic"] & @ mat // MatrixForm Or something more general: ArrayPad[#, {{0, 1}, {1, 2}} {{#, #}, {#2, #2}} & @@ Dimensions[#], "Periodic" ] & @ mat // MatrixForm


6

Another way (ArrayFlatten): ArrayFlatten[{ConstantArray[mat, n]}, 2]


6

Join[..., 2] will do it. Code for your case (and assuming n is not too large): Join[##, 2] & @@ ConstantArray[mat, n]


2

To get the exact same output as your code, where you use Orthogonalize on the eigenvectors, use this storeigenm1 = Join @@ Table[ Sort@Orthogonalize@Eigenvectors[n m1] , {n, 0, 3, .5}]; This is easier to understand, as it doesn't have the unneeded Transpose, SortBy, First, Last, or MapAt. But really, Eigenvectors by default returns normalized, ...


5

By default, all variables in Mathematica are assumed complex, and generic results are calculated if you don't specify any restrictions, e.g., with Assumptions. So the above result is generically true for complex matrices. But it's not true for non-generic matrices, where some elements have special values. For example, take ...



Top 50 recent answers are included