Tag Info

Hot answers tagged

24

The first step is to rasterize the points, so let's just start there as an example: n = 512; g = Image[Map[Boole[# > 0.001] &, RandomReal[{0, 1}, {n, n}], {2}]] The trick is to exploit the distance image. Almost all the work is done here (and it's fast): i = DistanceTransform[g] // ImageAdjust // ImageData; We need a little more precomputation ...


16

You can't define an unassigned symbolic variable throught itself. You are trying to do something like that: x = F[x] This is not right for symbolic computations, because x evaluates to itself as a pure symbolic value. Your code in FullFrom is: Equal[v, List[Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]]] So, you get the recursion. Try different ...


15

I know think of at least one way of doing it slowly and in a bitmap approach: img[p_, r_] := Module[{f, closest, color, colors, n, t}, n = 250; colors = List @@@ {Red, Green, Blue, Yellow, Orange, Pink, RGBColor[0, 0, 0], Cyan, Magenta, Brown, Purple}; color[i_] := Module[{c}, c = colors[[1 + Mod[i, Length@colors]]]; If[i == 0, {1, ...


13

Here is a very simple way to do it: Table[1/i! D[M, {a, i}] /. a -> 0, {i, 0, 3}] (* ==> {{{15, 0}, {0, 2}}, {{0, 1}, {1, 0}}, {{1, 5}, {-5, 0}}, {{0, 0}, {0, 0}}} *) This works even if the entries are not polynomials. If they are, you can replace the arbitrary maximum 3 in the Table index by the degree of the polynomial: Max[Exponent[M, a]] ...


13

If you want a random vector just because you need some arbitrary vector and you don't really care what it is, then Mr.Wizard's method of picking three random coordinates in [-1,1] will work. But if you care about the statistical properties of your vector, and in particular if you want it drawn from a uniform distribution over the surface of the sphere, then ...


11

David's answer has given the methods for producing random points that are uniformly distributed over the surface of the sphere. Of course, there are other probability distributions on the sphere that are of interest, as well as a number of methods for generating them. For instance, here is how to generate a random unit vector which follows the von ...


10

You should make use of Show and appropriate options e.g. we added PlotRange, AspectRatio etc. : Show[{ Plot[x^2, {x, -3, 3}, PlotRange -> 3, PlotStyle -> {Thick, Darker @ Green}], VectorPlot[{-y, x}, {x, -3, 3}, {y, -3, 3}]}, AspectRatio -> 1]


10

On this wikipedia page you find a collection of Tensor software and Mathematica has the biggest section. The package Ricci, which username acl pointed out in his answer is there, and I personally have used xAct. It looks like this And yes, as you suggest in your question, for smaller computation in specific dimensions you can also work in components ...


10

You could also use something like OverVector, since OverVector[v] = {Subscript[v,1],Subscript[v,2],Subscript[v,3]} isn't recursive, and so won't cause problems. This takes a bit more effort to input Type Ctrl+& and then Esc vec Esc to enter directly but if you're interested in the typeset forms it might be worth it.


10

You can switch off the 1/0 messages with Off[Power::infy] Now 1/0 only returns ComplexInfinity. If you want to intercept that (your "prevent the divide-by-zero operation from happening in the first place" seems to imply that) you'd have to redefine Power[0,-1]: Unprotect[Power] Power[0, -1] = ...; Protect[Power] with '...' a definition of your choice. ...


9

Here's one quick way for polynomial matrices: polyMat = {{15 + a^2, a + 5 a^2}, {a - 5 a^2, 2}}; Transpose[PadRight[CoefficientList[polyMat, a]], {2, 3, 1}] {{{15, 0}, {0, 2}}, {{0, 1}, {1, 0}}, {{1, 5}, {-5, 0}}} Alternatively (as Jens hints), you can do Flatten[PadRight[CoefficientList[polyMat, a]], {3}]. You can check that the matrix polynomial is ...


9

Define the inner product modulo $2$: orthogonalize[a_] := Mod[Orthogonalize[a, Mod[#1.#2, 2] &], 2] Example: (m = Union[orthogonalize[RandomInteger[{0, 1}, {500, 64}]] ]) // ArrayPlot Check orthogonality: Mod[m . Transpose[m], 2] // ArrayPlot


9

Assuming that the lengths of the lists are integer multiples of each other, you could do this: x = RandomInteger[{0, 100}, 100]; y = RandomInteger[{0, 100}, 200]; Mean /@ Partition[y, Length[y]/Length[x]] (* ==> {37/2, 79, 23, 58, 111/2, 59, 143/2, 45/2, 51, 123/2, 57, \ 76, 43, 111/2, 93/2, 33, 97/2, 37/2, 51, 151/2, 43, 7/2, 191/2, \ 123/2, 44, 49, ...


8

Solution You could use Graphics, Arrow and s which is a scaling factor and write arrows = {{{0, 0}, {1, 1}}, {{1, 0}, {1, 0}}, {{2, 0}, {0, 1}}, {{0, 1}, {1, -1}}, {{1, 1}, {1, 1}}, {{2, 1}, {-1, 1}}, {{0, 2}, {0, 1}}, {{1, 2}, {-1, 0}}, {{2, 2}, {0, -1}}}; s = 0.3; Graphics[{Arrow[{#1, #1 + s*Normalize[#2]}] & @@@ arrows, Red, ...


8

Let us first define the vector field A = {10 x, 20 y^3, 30 z}; and load the vector analysis package: << VectorAnalysis` SetCoordinates[Cartesian[x, y, z]]; Now let's define $A\cdot \nabla A$ field = (A.Grad[#]) & /@ A (* ==> {100 x, 1200 y^5, 900 z} *) and plot both fields: pl1 = VectorPlot3D[A, {x, 0, 1}, {y, 0, 1}, {z, 0, 1}, ...


8

Here is a way to do all the things you asked for automatically, independently of Mathematica version. The approach relies on a special symbol to identify when we're dealing with a vector: Instead of using things like x, y etc. for vectors, the convention now is that vectors are written as vec[x], vec[y], etc. You could also define the wrapper OverVector[x] ...


8

As I interpret the question I recommend that you use Interpolation for this due to its versatility and configurability. Here is some example data: x = Table[Sin[x], {x, 0, 7 Pi, 7 Pi/99}]; y = Table[Cos[x], {x, 0, 7 Pi, 7 Pi/236}]; {lnx, lny} = Length /@ {x, y} {100, 237} Now create a mapping and plot it: yint = Interpolation[y, InterpolationOrder ...


7

First, if you don't know the degree you can compute it: m = {{15 + a^5, a + 5 a^2}, {a - 5 a^2, 2 c}}; degree = Max[Flatten[Exponent[#, {a}] & /@ Flatten[m]]] (I have increased the degree of m to verify that intermediate zero matrices are correctly output.) And now, with degree in hand, why not use a function intended for this task? Coefficient[m, ...


7

This is similar to J. M.'s solution, but generalized to rank $n$ tensors. Clear[decomposePolyMat] decomposePolyMat[m_, var_] := Module[{rank = ArrayDepth@m, coeffs = CoefficientList[m, var],len}, len = Max@Map[Length, coeffs, {rank}]; Flatten[Map[PadRight[#, len] &, coeffs, {rank}], {rank + 1}] ]


7

It helps to do a little analysis to simplify the problem. This expression is integrating over a line through $\mathbf{b}$ in the direction of $\mathbf{a}$. By choosing a suitable coordinate system you can arrange for $\mathbf{a} = (x,0,0)$ where, to assure a unit Jacobian, $x = \|\mathbf{a}\|$ (and you can even make $\mathbf{b} = (0,b,0)$ if you like, but ...


7

Technically, the normal distribution is defined on the real line, $\mathbb{R}$, but vector lengths are nonnegative numbers, elements of $\mathbb{R}_{0+}$. So you can't really have the lengths of your vectors satisfy a normal distribution. You have to choose a distribution for the lengths that has the correct domain, $\mathbb{R}_{0+}$. One thing you can do, ...


7

If you have Mathematica Version 9, you can use Vectors and TensorReduce: Assuming[(x | y) \[Element] Vectors[n] , TensorReduce[Dot[x, y] - Dot[y, x]]] (* 0 *) TensorReduce[Dot[x, y] - Dot[y, x], Assumptions -> (x | y) \[Element] Vectors[n]] (* 0 *) TensorReduce[Cross[x + y, z], Assumptions -> (x | y | z) \[Element] Vectors[n]] (* x\[Cross]z + ...


7

To summarize my comments to the question: In order to get a normalized version of your (real) vector, just use Normalize: v=Normalize[{1,2,3}] Note that you can also use other norms in Normalize, see the documentation for that. I shall assume you want the Euclidean one. Assume we consider a general normalized vector v= Normalize[{x, y, z}] In order ...


6

Mathematica does not yet support tensor calculus and notation natively, but there are numerous 3rd party packages which address this issue very well. This is a list of the packages I am more familiar with: xAct suite: free (GPL license), most powerful, created by relativists, actively supported. Disclosure: I contributed to the suite by creating the ...


6

vectors = RandomReal[{-1, 1}, {100, 2}]; scaled = Map[Norm[#] # &, vectors]; You can use Nest to do it many times: scaled = Nest[Map[Norm[#] # &, #] &, vectors, 5]; ListPlot[{vectors, scaled}] Oh, and to multiply two lists together element-wise just use * norms = Norm /@ vectors; norms*vectors == Map[Norm[#] #&, vectors] (* True *)


5

The formula for a polygon's centroid is easily expressed in Mathematica: PolygonCentroid[pts_?MatrixQ] := With[{dif = Map[Det, Partition[pts, 2, 1, {1, 1}]]}, ListConvolve[{{1, 1}}, Transpose[pts], {-1, -1}].dif/(3 Total[dif])] This works for any Polygon[], not just quadrilaterals.


5

You said "field", so presumably you want q to be a prime. Try the following. I've changed the name from Span to columnSpace because the former is a reserved Mathematica name. The vectors here are the columns of a given matrix (the typical situation); if you want to make them rows instead, the changes are fairly obvious. The key is to use RowReduce with ...


5

As Daniel commented, you can restrict the scope of Quiet with its second argument: Quiet[{1, 2, 3, 4}/{5, 6, 0, 8}, Power::infy] Other messages are still printed: Quiet[Divide[1, 2, 3], Power::infy] During evaluation of In[2]:= Divide::argrx: Divide called with 3 arguments; 2 arguments are expected. >>


5

Since you are asking for "a way to prevent the divide by zero operation from happening in the first place," let me suggest another approach that is just slightly more involved than the obvious answer (i.e., just don't include zeros in b): Lets' pick two lists that cover various cases with zeros: a = {1, 2, 3, 4, 0, 0}; b = {5, 6, 0, 8, 1, 0}; Now I'll ...


5

First off, two suggestions: I would strongly recommend not using subscripts the way you have used. They can lead to several issues if you're not aware of how subscripts are handled (for instance, when you try to clear them). It is best to use them only for typesetting purposes. Use DownValues instead. Use Block to localize x so that it is not affected by ...



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