Hot answers tagged

44

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


38

Edit: I added more explanations below, because this visualization method is quite different from conventional vector plots For just this purpose I had at some point invented the following visualization technique. I'll reproduce your definition first. It defines a complex vector field on the surface of a unit sphere. Clear[ϵ];(*Polarization vector*)ϵ[λ_] = ...


26

Nice "inverse problem". The angle distribution looks to me like a Gaussian. ϕ[x_, y_, z_] := (Pi Exp[-Dot[{x, y}, {x, y}]/2] - Pi/2); V[x_, y_, z_] := Evaluate[Simplify[ComplexExpand[-RotationMatrix[ϕ[x, y, z], {y, -x, 0}].{x, y, 0}/Sqrt[{x, y}.{x, y}]]]]; V[0, 0, z_] := {0, 0, -1}; R = Pi; P = Select[Flatten[Table[Table[{x, y, 0}, {y, -R, R, R/20}],{x, -R,...


25

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


24

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


24

I recommend using Transpose twice since it is more efficient than other approaches. Moreover Plus has the Listable attribute, thus one need not map Plus over a list (vector). Transpose[v1 + Transpose[v2]] {{a + d, b + e, c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}} Having said that remember that one can rewrite it very concisely in the Front-End:...


23

Here are two suggestions for the function f[z_] := 1/z; First, instead of defining a region to omit from your plot, you should base the omission criterion on the length of the vectors (so that you don't have to adjust the criterion manually when switching to a function with different pole locations). That can be achieved like this: With[{maximumModulus = ...


21

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


20

Maybe you can transform this into an easier problem: If you were instead looking for lines that are at any point perpendicular to your vector field: (that's easy, it's just a coordinate transformation) and if your (transformed) vector field were the gradient of a scalar field (yes, big if), then this would be really easy: These lines are simply the level ...


20

EDIT As OP wishes (and as Rahul correctly points out) my original answer puts unit circle in TB plane (my error as labels suggest) and what is desired is TN plane. pp = ParametricPlot3D[r[t], {t, 0, 2 \[Pi]}] fs = FrenetSerretSystem[r[t], t]; tan[s_] := fs[[2, 1]] /. t -> s nrm[s_] := fs[[2, 2]] /. t -> s cir[u_] := Table[r[u] + Cos[j] tan[u] + (...


19

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


19

The answer depends a lot on what you mean by "doing" vector calculus. You want results to be displayed without using component notation, and that's in general a difficult thing to achieve. A prerequisite about doing completely symbolic vector calculus is to define the simplification rules. But even in "non-vector" algebra it's often hard to get Simplify to ...


19

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


19

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


18

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], Hold@Sequence[0, 0] ...


17

You are treating $A$ and $B$ as column vectors and combining them column to column, but if you just bracket the two vectors, you are combining them row to row. A final Transpose will do the trick. A = {a, b}; B = {c, d}; NotYourResult = {A,B} Result = Transpose[{A, B}] where NotYourResult is assigned {{a,b},{c,d}} or $\left[ {\begin{array}{cc} a & ...


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

Following this question you can define: invmollweide[{x_, y_}] := With[{theta = ArcSin[y]}, {Pi (x)/(2 Cos[theta]), ArcSin[(2 theta + Sin[2 theta])/Pi]}]; fc[phi_] := Block[{theta}, If[Abs[phi] == Pi/2, phi, theta /. FindRoot[2 theta + Sin[2 theta] == Pi Sin[phi], {theta, phi}]]]; cart[{lambda_, phi_}] := With[{theta = fc[phi]}, {2/Pi*lambda Cos[...


16

It's already built-in. It's called Cross. Cross[{1, 2}] Output is {-2, 1}


15

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


15

Update V.11.3: In version 11.3+ the new option VectorMarkers can be used with Placed to control the position of vectors: points = Tuples[{-1, 1}, {2}]; Row[VectorPlot[{-1 - x^2 + y, 1 + x - y^2}, {x, -2, 2}, {y, -2, 2},  VectorPoints -> points, VectorMarkers -> Placed["Arrow" , #], VectorScale -> {.5, .4}, ImageSize -> 300,   Prolog ...


15

To achieve what you need requires to distribute the sum over v2: (v1 + # &) /@ v2 which is a short form of: Map[ v1 + # &, v2 ]


14

Norm in general assumes complex arguments and uses Abs to provide for that: Norm[{x, y}] Sqrt[Abs[x]^2 + Abs[y]^2] For real elements, you can either add an assumption for Simplify or manually get rid of Abs beforehand: Simplify[Norm[{Cos[t], Sin[t]}], Element[t, Reals]] Simplify[Norm[{Cos[t], Sin[t]}] /. Abs -> Identity] 1 1 This now ...


13

You could remove the zero from the denominator, and the corresponding entry from the numerator: a = {1, 2, 3, 4}; b = {5, 6, 0, 8}; Pick[a, Positive[b]]/Pick[b, Positive[b]] (* ==> {1/5, 1/3, 1/2} *)


13

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


13

The option VectorPoints determines how many vector boxes there are. The plot domain is subdivided into a grid, whose grid points span the plot domain in each direction. Equal-size rectangular boxes surround each grid point so that the boxes are adjacent and tile the plot range (ignoring any padding). Here is a picture with VectorPoints -> 9 (the ...


13

Add this to your notebook or init file $PrePrint = If[MatrixQ[#], MatrixForm[#], #] &; Then all matrices will automatically display as MatrixForm and If you want to format lists as column vectors also, try $PrePrint = Which[MatrixQ[#], MatrixForm[#], VectorQ[#], ColumnForm[#], True, #] &; Now also


13

The definition of the scalar product in your question assumes that all your kets are orthogonal unit vectors. In that case, the most natural approach would be to use the built-in Bra and Ket as follows: Ket /: Dot[Bra[x__], Ket[y__]] := Times @@ MapThread[KroneckerDelta, {{x}, {y}}] BraKet[x_, y_] := Bra[x].Ket[y] Bra[2, 4].Ket[2, 4] (* ==> 1 *) ...


13

Accumulate@Array[b, {3}] (* {b[1], b[1] + b[2], b[1] + b[2] + b[3]} *) therefore: {a, b} = Transpose[list]; Transpose[{a, Accumulate[b]}] Also this will do the job: Rest@FoldList[{#2[[1]], #1[[2]] + #2[[2]]} &, {0, 0}, list] or even easier list[[All,2]]=Accumulate@list[[All,2]]; list


13

expr = Norm[{a, b*c}] Sqrt[Abs[a]^2 + Abs[b c]^2] Since ComplexExpand assumes all its variables to be real, we automatically get what we want. ComplexExpand@expr Sqrt[a^2 + b^2 c^2] Other methods include Refine[expr, {a > 0, b c > 0}] Sqrt[a^2 + b^2 c^2] and FunctionExpand[expr, {a > 0, b c > 0}] Sqrt[a^2 + b^2 c^2]


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