Skip to main content

Posts

syntax - Why doesn't PatternTest work with Composition?

While playing around with the solutions to this question , I've found some very strange behaviour: MatchQ[3,_?Composition[Not,OptionQ]] (* ==> False *) MatchQ[3,_?(Not[OptionQ[#]]&)] (* ==> True *) Composition[Not,OptionQ][3] (* ==> True *) So what's wrong with the first pattern? Or did I just find some bug? Answer Because PatternTest binds very tightly. You need extra parentheses: MatchQ[3, _?(Composition[Not, OptionQ])]

plotting - Making a ListVectorPlot3D from data in an external file

I have a .dat file with six columns: {x y z vx vy vz} , where x, y, z are space coordinates and vx, vy, vz are the vector components. How can I use this file to get a ListVectorPlot3D image? Because I tried to arrange the data as {{x,y,z}, {vx,vy,vz}} and Mathematica still gives me an error message: ListVectorPlot3D::vfldata ... is not a valid vector field dataset or a valid list of datasets. I haven't found many related question about this, just one about using Graphics and drawing each vector, but I think it would be simpler to use ListVectorPlot . Answer I imported your data data = Import["http://pastebin.com/download.php?i=VByC3ZEg", "Table"]; and transformed it into a vector field, deleting duplicated entries: vecdata = Partition[#, 3] & /@ DeleteDuplicates[data]; As noted in a comment, the base points all lie in the xy-plane (and the z-components of the vectors are nearly the same): vecdata[[All, 1, 3]] // Union {0.} vecdata[[All, 2, 3]] // Union...

front end - How can I easily eliminate the dependency of a stylesheet on a non-built-in stylesheet?

As per discussion here : User A has a notebook that uses custom definitions from styles.nb. Now user A passes the notebook to user B. The styles do not show up for B because B does not have styles.nb. This is because the private stylesheet for the notebook inherits from styles.nb (which inherits from Default.nb). How can I easily copy the definitions from style.nb into the private styles for the notebook i.e. as if style.nb didn't exist and all custom definitions were in the private styles and base inherited from Default.nb Answer In answering the question the underlying assumptions are that the parent stylesheet (myparent.nb) of private stylesheet for a notebook is not a built in stylesheet; and that the stylesheet has one or more custom styles that inherit from myparent.nb. ClearAll[stylesheetSetter]; stylesheetSetter[path_String] := Module[{child, childstyles, parent, nb, tmp, parentparent, parentstyles}, (* find the parent stylesheet from the private stylesheet, a.k.a chi...

graphics - How can this confetti code be improved to include shadows and gravity?

Here's some confetti: Graphics@Table[{ RGBColor @@ RandomReal[{0, 1}, 3], Translate[#, RandomVariate[NormalDistribution[], 2]] &@ Rotate[#, RandomReal[{0, 2 \[Pi]}]] &@ Scale[#, .1] &@ GeometricTransformation[ #, ShearingTransform[ RandomReal[{-45, 45}] Degree, {1, 0}, {0, 1} ] ] &@ Translate[Rectangle[], {-.5, -.5}] }, {1000}] How can this code be improved, for example, by including shadows, raytracing or the effects of gravity to make it more realistic? Answer How can this code be improved, for example, by including shadows, raytracing or the effects of gravity to make it more realistic? I felt that this question deserved an answer. The one I describe here is to create a set of confetti "agents" that respond in quasi-physical ways to external forces and "know" how they should be displayed. It is handy, and a whole lot of fun, to do this in an extensible and easily mod...

How to scramble a string of text?

How to implement foo function which works like this: foo @ "abcdef" "fdbace" Steps: "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace" Reverse the first two letter of the string "abcdef" which will give "bacdef" . Then take this as the new string and reverse the first 3 letter. Similarly, proceed for all the characters of the string. Answer s = "abcdef"; FoldList[ StringJoin[ StringReverse[StringTake[#1, {1, #2}]], StringTake[#1, {#2 + 1, -1}] ] &, s, Range[2, StringLength[s]] ] {"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"}

rule - Difference between Association and Dispatch

In the example below, Association has a different behavior from Dispatch : {1, 2, 3} /. Association@{1 -> "test",2 -> "test" , _Integer -> Null} {1, 2, 3} /. Dispatch@{1 -> "test" ,2 -> "test" , _Integer -> Null} {"test", "test", 3} {"test", "test", Null} The pattern _Integer -> Null is not applied on the first case. The question is : There is some way to efficiently make Association behave like Dispatch ? Or Dispatch is the best solution for this case? PS: this is a toy code, my original list if much bigger and used a lot o times, so a hash table is necessary. Answer General If you are not going to change your list of rules after you construct them, Dispatch is pretty good. From the user's viewpoint, the main difference is that it is cheap to add new key-value pairs to associations, or remove existing ones, constructing new associations. Not so with Dispatch - once you obta...

differential equations - Increasing MaxPoints in NDSolve results in memory issue

I am interested in increasing "MaxPoints" in NDSolve's "MethodOfLines" in attempt to increase the resolution of the plot of the solution of linearly damped wave equation with transparent boundary conditions and square pulse initial condition. Here is my code: interpolatingFunctLinear[initalPulseFunction_, xBoundLow_, xBoundHigh_, timeBound_] := First[ pdeY = D[Y[x, t], t, t] + .04 D[Y[x, t], t] == D[Y[x, t], x, x]; solnDerivativeY = NDSolve[{pdeY, Y[x, 0] == initalPulseFunction, Derivative[0, 1][Y][x, 0] == 0, Derivative[1, 0][Y][xBoundLow, t] == Derivative[0, 1][Y][xBoundLow, t], Derivative[1, 0][Y][xBoundHigh, t] == -Derivative[0, 1][Y][xBoundHigh, t]}, Y, {x, xBoundLow, xBoundHigh}, {t, 0, timeBound }, Method -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "MaxPoints" -> 2000}}]] Here is my square wave: Piecewise[{{1 , Abs[x] 8.40749}}] Now running the whole thing together, we get the...