Tag Info

21

There is an appropriate metrics: HammingDistance[ab, ac] 1 one could use also (but in general it yields different results since it counts transpositions, deletions etc.) DamerauLevenshteinDistance[ab, ac] 1

20

Clip is usually quite fast: m = RandomReal[{-10^6, 10^6}, {3, 3}]; neg = Clip[m, {-Infinity, 0}] pos = Clip[m, {0, Infinity}] (*{{0., -181286., -442666.}, {0., -233694., -847828.}, {-128249., 0., -540037.}} {{947792., 0., 0.}, {755278., 0., 0.}, {0., 63058.1, 0.}}*) neg + pos == m True

19

Time-dependent case in the time-dependent case, $[H(t),H(t')]\neq0$ in general and we need to time-order, ie, the operator taking a state from $t=0$ to $t=\tau$ is $U(0,\tau)=\mathcal{T}\exp(-i\int_0^\tau dt\, H(t))$ with $\mathcal{T}$ the time-ordering operator. In practice we just split the time interval into lots of small pieces (basically using the ...

15

For Integer data we also could write: Tr @ Unitize @ BitXor[ab, ac] 1 For Real data we can use the slightly slower but also shorter: Tr @ Unitize[ab - ac] Blackbird challenged me to provide a method that works on all input types. My approach is to select between methods depending on data. diff[a__?(VectorQ[#, IntegerQ] &)] := Tr @ Unitize ...

14

The rank of a matrix is typically determined by performing a Gaussian elimination and is given by the number of non-zero rows. In your second case, the large number $5.4\times 10^{12}$, when eventually used as a pivot, gives a badly conditioned matrix (myM2 is the second matrix in your question): RowReduce[myM2] RowReduce::luc: Result for RowReduce of ...

13

This might be as good a time as any to distill the collective wisdom of Messrs. Huber, McClure, and Toad R. M. As already mentioned, there is this quantity of great interest to people in the business of solving simultaneous linear equations, called the condition number, and conventionally denoted by the symbol $\kappa$. This is usually associated with a ...

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

You could also use the functions Positive and Negative: m = RandomInteger[{-10, 10}, {10, 10}]; pos = m Boole[Positive[m]]; neg = m Boole[Negative[m]]; give the positive and negative portions. As becko points out, replacing Boole[Positive[mat]] with UnitStep[m]: pos = m UnitStep[m]; neg = m UnitStep[-m]; is even more succinct. These can even be ...

12

So you need two thing. Six matrices and and a cube with each side painted with a matrix. In such wall painting Texture is the most handy option. Let me show an example. n = 3; (*Matrix Dimension*) color = {Red, Blue, Green, Yellow, Orange, White}; Table[mat[k] = Grid[Table[RandomInteger[{1, n^2}], {i, 1, n}, {j, 1, n}], Frame -> All, ...

12

Using Replace (assuming you only want to replace on level 2, as you mention "matrix"): Using Except My first version (I kept this version to point out the usage/impact of Orderless) Replace[SIGMA, Except[HoldPattern[___ x^2 ___]] -> 0, {2}] {{0, b x^2}, {0, 0}} Improved version, thanks to Leonid Replace[SIGMA, Except[___ x^2] -> 0, {2}] as ...

11

Looking at CompilePrint[compiledGlynnAlgorithm] there are some CopyTensor in it which aren't really needed. There's also a few CoerceTensor in there when it might be faster to just coerce the integer matrix once at the beginning. By slightly adjusting the function all CopyTensor and CoerceTensor go away giving a small increase in speed: ...

10

The question is, what do you want to do with the output. The output of Position is in a form so that it can directly be used with Extract list = {a, b, a, a, b, c, b}; pos = Position[list, b]; Extract[list, pos] (* {b, b, b} *) For this simple example, it is a bit useless because we already know, that on all positions pos we have a b in list. ...

10

If you make all the component parts matrices, you can use ArrayFlatten c = {{{{1}}, {{2, 3, 4}}}, {{{5}, {9}}, {{6, 7, 8}, {10, 11, 12}}}}; c // MatrixForm ArrayFlatten[c] // MatrixForm

10

You can also achieve your ultimate (or ulterior) goal with ImageResize: imgdata = Array[0.2 #1 + 0.1 #2 - 0.2 + 0.01 #3 &, {2, 2, 3}] (* {{{0.11, 0.12, 0.13}, {0.21, 0.22, 0.23}}, {{0.31, 0.32, 0.33}, {0.41, 0.42, 0.43}}} *) img = Image @ imgdata; img2 = ImageResize[img, Scaled[2]]; ImageData[img2] // MatrixForm P.S. There are various Resampling ...

10

Why complicate it? a[[3 ;; 4, 2 ;; 4]] = b; Here is a general function. It take a main matrix, and a sub matrix. It puts the sub matrix inside the main matrix. All what you have to do is just tell it the starting row number and starting column number for where to insert the sub matrix at. Updated: Added pattern checking on arguments. Added additional ...

10

Let me put my comment into an answer, because I think we might have misunderstood each other. You answered in the comment However, it would be great to do whole process (preparation of the matrix, solving the eigensystem, and further analysis) in Mathematica. That exactly was my idea. You only write some lines of C-Code which are compiled into a ...

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

Since there hasn't been any discussion of NDSOlve yet, let me point out that for a finite-dimensional Hilbert space where the Schrödinger equation is merely a first-order equation in time, it's easiest to just do this (using the two-dimensional Hamiltonian ham from acl's answer): ham[e1_, e2_, b_, omega_, t_] := {{e1, b*Cos[omega*t]}, {b*Cos[omega*t], ...

9

Be explicit and do it in two steps. The first step is just the series computation with the matrix expression M+S replaced by a single variable: f = Series[(1 + t x)^(-1), {t, 0, 3}] $1-x t+x^2 t^2-x^3 t^3+O[t]^4$ We need to describe how to expand powers of x. This can be done recursively: power[a_, n_Integer] /; n > 1 := Distribute[a . power[a, ...

9

As $P$ is explicitly constructed from eigenvectors of a self-adjoint matrix, it is unitary, i.e $P P^\dagger = I\qquad$ where the $\dagger$ is the conjugate transpose (or Hermitian conjugate, if you prefer). So, calculating the inverse is simply ConjugateTranspose[P] which is much faster than calculating it using Inverse. That said, you have to ensure that ...

9

My first version had to be repaired because I didn't think about the orientation of the faces, and I cheated... faces = First@Normal[PolyhedronData["Cube", "Faces"]]; grids = ImagePad[ Rasterize[#, ImageSize -> 400], 10, Padding -> White] & /@ Table[ Grid[ ConstantArray[i, {4, 4}], ItemStyle -> {Automatic, ...

9

Based on the comments, Listable is a possible way for you. Thus, you could: SetAttributes[f,Listable] and then simply: f[m1,m2] to obtain: {{f[a1, a2], f[b1, b2]}, {f[c1, c2], f[d1, d2]}} EDIT To apply this on a built-in (non-Listable function) like List on could do, as noted by @rcollyer below: f[m1,m2]/.f->List (please also note his ...

9

You could do this ab = {1, -1, -1, -1, 1, 1}; ac = {1, -1, -1, 1, 1, 1}; EditDistance[ab, ac] which would give a result even if the lists had different lengths (or whatever). The documentation says: EditDistance[u, v] gives the number of one-element deletions, insertions, and substitutions required to transform u to v.

8

The group has 6048 elements. (Could it be isomorphic to $U_3(3)$?--see below.) count = 0; (matrices = NestWhile[(Print[count++]; Union[#~Join~Flatten[Outer[Dot, {gMatrix, hMatrix, kMatrix}, #, 1], 1]]) &, {IdentityMatrix[7]}, Length[#2] != Length[#1] &, 2, 99]) // Length // Timing $\{2.2, 6048\}$ This code ...

8

For full control over the plot and the analysis, it is useful to know how to do the calculations yourself. They include: Finding the contour level corresponding to the desired confidence (without this the result scarcely can be called a "confidence" ellipse!); Determining the limits (and aspect ratios) of the plot; Establishing a useful mesh on the ...

8

You can use Normalize with its second argument for this purpose: (mat = Normalize[#, Total] & /@ Transpose@L // Transpose) // MatrixForm Instead, if you were normalizing the rows by the sum of their elements, you could simply leave out the transposes and do mat = Normalize[#, Total] & /@ L or even mat = #/Tr@#& /@ L For your specific ...

8

Calculating eigenvalues involves solving for the roots of the characteristic polynomial, which is of degree equal to the order of the size of the matrix. When you input real numbers, it can search for the roots of the polynomial using numerical techniques. When you input exact integers (or rationals, probably) it tries to find exact answers for the roots of ...

8

Using the basic approach of applying a function to the first level in a list (i.e. the 'rows' in a matrix) you have : #/Norm[#] & /@ evecs As CoreyKelly pointed out though, in your case you are just normalizing the elements, so you can apply the Normalize function : Normalize /@ evecs

8

This appears to be a genuine bug with exact arithmetic in Eigensystem. Here is a comparison to the same calculation with real numbers, for which I use the matrix mat//N: mat = {{7/2 - I/2, -1 + I, 1/2 + 5 I/2}, {-1 + I, 5 + I, -1 + I}, {1/2 + 5 I/2, -1 + I, 7/2 - I/2}}; {vals, vecs} = Eigensystem[mat] (* ==> {{6, 3 + 3 I, 3 - 3 I}, {{1, -2, 1}, ...

8

Here's an alternative approach. A matrix with integer values that has an inverse that's also integer-valued is called a unimodular matrix. I don't know how to generate unimodular matrices directly, but an indirect way is to use the Hermite decomposition, which decomposes any matrix into a unimodular matrix and an upper triangular matrix. For instance: m = ...

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