Tag Info

26

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 ...

17

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 ...

17

There are many ways, for example: n = 5; l = SparseArray[i_?(Divisible[#, 3] &) -> i/3 x, {3n}, 0] SparseArray[<5>,{15}] List @@ l {0, 0, x, 0, 0, 2 x, 0, 0, 3 x, 0, 0, 4 x, 0, 0, 5 x} (*earlier Array[# x &, n], Range thanks to Simon Woods's, tom's and Mr. Wizard's comment*)  Riffle[Range[x, n x, x], ...

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 ...

16

When n is large and x is known, using a PackedArray may be a good option. ar3=ConstantArray[0,3n]; ar3[[3;;;;3]]=Range[x, n x, x]; ar3 To see that the result is a PackedArray, we see that << Developer; PackedArrayQ[ar] True Whereas for Kuba's last array we would have False (even if x has a value). Note that ConstantArray also produces a ...

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, ...

14

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 ...

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]] ...

11

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]

11

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. ...

11

Just for fun: Animate[Show[ ParametricPlot3D[r[t], {t, 0, u}, PlotRange -> {{-10, 25}, {-10, 10}, {-10, 15}}], Graphics3D@Sphere[r[u], 1]], {u, 0, 10 Pi}] I'm bored: Animate[Show[ ParametricPlot3D[r[t], {t, 1/100, u}, PlotRange -> {{-10, 25}, {-12, 10}, {-10, 15}}], Graphics3D[Table[{Opacity[1 - h/u], Sphere[r[u - h], 1]}, {h, 0, u - ...

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

Assuming that we have three-dimensional real vectors : $Assumptions = (u | v | w) ∈ Vectors[3, Reals]; we can use e.g. various tensor functions (new in ver. 9) e.g. TensorReduce to reduce (simplify) a tensor expression, e.g. TensorReduce[ v.v + w.w - (v + w).(v + w) ] TensorReduce[u \[Cross] (v \[Cross] w) ] -2 v.w -w u.v + v u.w We can perform ... 10 In Memory: Table is handy. v[x_, n_] := Flatten@Table[{0, 0, x i}, {i,n}] v[x,5] {0, 0, x, 0, 0, 2 x, 0, 0, 3 x, 0, 0, 4 x, 0, 0, 5 x} Or {0, 0, x #} & /@ Range@5 // Flatten Minimal Memory Here is an interesting approach if you want to access very large arrays without the memory overhead, create a function to return elements from an ... 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 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, ... 9 A new solution I realized that comparing each and every value in the sections might be inefficient, especially in cases where the sections are long. Instead we need only the relative ordering of these elements from which we can compute the number of Less pairs. Here is my solution: Edit: Ray Koopman provided a greatly improved counting method (applied to ... 9 I would check out first Michael Trott blog: On the Importance of Being Edgy—Electrostatic and Magnetostatic Problems with Sharp Edges. From that you can easily construct: \[Phi]LineSegment[{x_, y_, z_}] = WolframAlpha[ "electric potential of a charged line segment", {{"Result", 1}, "Input"}][[1]] /. {Subscript[\[Epsilon], 0] -> 1/(4 Pi), l ... 8 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 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 ... 8 After testing a number of different methods I have found this code to be the fastest on packable data: Flatten[ArrayPad[{Range[x, x n, x]}, {{2, 0}, 0}]\[Transpose]] And this fastest on unpackable data: Module[{rs = ConstantArray[0, 3 n]}, rs[[Range[3, 3 n, 3]]] = Range[x, x n, x]; rs] SparseArray generation seems to be the fastest with this form but ... 8 I'm going to assume that "everything" includes some simple ContourPlot and StreamPlot attempts, but that you are getting mired in the style details of your plots (missing streamlines, streamlines not where you want them, colors, etc.) To draw the charge distributions, we can use Graphics. gP1 = Graphics[{PointSize[Large], Point[{14, 10}]}]; gP2 = ... 8 How about writing the Gram-Schmit down by yourself? GramSchmidt[w_?MatrixQ] := Module[{v = ConstantArray[0, Length[w]]}, Table[ v[[n]] = w[[n]] - Sum[(v[[i]].w[[n]]/v[[i]].v[[i]])*v[[i]], {i, 1, n - 1}], {n, 1, Length[w]}]; v ] Then you have the unscaled orthogonal basis GramSchmidt[{{1, 1, 1}, {1, 1/2, 1/3}, {1, 2, 3}}] ... 8 Let's make r[t] and c[t] depend on time step t. Suppose we have 5 robbers and 3 cops, and their initial positions are: r[0] = RandomReal[10, {5, 2}] c[0] = RandomReal[10, {3, 2}] Given the current position x of a robber and a list y of that of the cops, define the robber's next step (the arrow) to be: step[x_, y_List] := Normalize@Sum[(x - y[[i]])/Norm[x ... 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}] ]

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