Skip to main content

Posts

output formatting - Question about evaluation control

I want to make a function that takes a function as a parameter and prints an integral with the function inside. I've tried this: L[f_, a_, b_] := HoldForm[Integrate[f[x], {x, a, b}]] L[E^-Sqrt[#] &, 0, 1] But it gives me this: $$\int_0^1 \left(e^{-\sqrt{\text{#1}}}\text{&}\right)[x] \, dx$$ I want the integrand to look normal, that's all, so I want the variable x to be substituted inside it, but not evaluated any further. Is this possible? Answer This works nicely: L[f_, a_, b_] := HoldForm[Integrate[#, {\[FormalX], a, b}]] &[f[\[FormalX]]] Note that I used \[FormalX] to prevent conflicts with the usual x , which may have had a previous definition. Try L[E^-Sqrt[#] &, 0, 1] with this definition:

Truncating a list once all members of a set have appeared?

Consider if you would the case where we have some list of elements: list0 = {54, 4, 7, 9, 3, 54, 4, 20, 2, 456, 2, 3}; And we have some target list: targetList = {2, 3, 4, 7}; We'd like to scan from left to right through list0 and chop off an RHS tail at the moment all elements in targetList have appeared. With the example given we would have: list0chopped = {54, 4, 7, 9, 3, 54, 4, 20, 2}; As an output. Is there a simple way to do this? Answer I propose: truncate[a_List, b_List] := a ~Take~ Max @ Lookup[PositionIndex[a][[All, 1]], b, 0] For maximum performance replace PositionIndex with cleanPosIdx from Why is the new PositionIndex horribly slow? I'll add comparative timings later if I get the chance.

plotting - How to embed graphic-element-specific metadata within a Graphics object?

One of the most crucial requirements for rich, interactive scientific graphics is being able to "annotate" individual graphic elements (e.g. the data points of a scatterplot, the individual curves in a plot of multiple curve fits to data, individual subregions of a density plot) with additional information. I'm using the term "annotate" very broadly here to stand for "associate additional information with". Minimally, one would like to be able to assign unique identifiers to each feature of interest, that can be used as keys in auxiliary data structures. (For example, one way to make a scatterplot interactive is to equip each data point in the scatterplot with a tooltip that will display additional information about that point when the user hovers the cursor over the point. Another way would be to give the user the ability to "light up" a subset of the data points based on some shared metadata value.) Can the Mathematica Graphics object acco...

numerics - Does Mathematica have an equivalent of C's nextafter?

In C (and many other programming languages), there is a function double nextafter(double x, double y) which takes two (IEEE 754) floating-point numbers and returns the next representable floating-point number after x in the direction of y . What is the Mathematica equivalent of this function for MachinePrecision numbers? EDIT: This issue has been brought up in the comments, but for the benefit of future readers, note that this is a substantially more subtle task than simply adding or subtracting $MachineEpsilon . The problem is that the distance between one floating-point value and the next changes with magnitude. $MachineEpsilon , by definition, is the smallest positive floating-point value such that 1.0 + $MachineEpsilon > 1.0 . The distance between 1.0e-300 and the next number up will be much smaller, while the distance between 1.0e+300 and the next number up will be much greater. In addition, there are issues raised by the transitions between one order of magnitude and th...

differential equations - Position of discontinuous coefficient influences the solution of PDE

This issue is raised in the discussion under this post about heat flux continuity and I think it's better to start a new question to state it in a clearer way. Just consider the following example: Lmid = 1; L = 2; tend = 1; m[x_] = If[x eq1 = m[x] D[u[x, t], t] == D[u[x, t], x, x]; eq2 = D[u[x, t], t] == D[u[x, t], x, x]/m[x]; Clearly, eq1 and eq2 is mathematically the same, the only difference between them is the position of the discontinuous coefficient m[x] . Nevertheless, the solution of NDSolve will be influenced by this trivial difference, if "FiniteElement" is chosen as the method for "SpatialDiscretization" : opts = Method -> {"MethodOfLines", "SpatialDiscretization" -> {"FiniteElement", "MeshOptions" -> {"MaxCellMeasure" -> 0.01}}}; ndsolve[eq_] := NDSolveValue[{eq, u[x, 0] == Exp[x]}, u, {x, 0, L}, {t, 0, tend}, opts]; {sol1, sol2} = ndsolve /@ {eq1, eq2}; Plot[{sol1[x, tend],...

simplifying expressions - How to simplify exponents of the form $expleft(ifrac{2pi}{N}(N+1)right)$ inside a function

I'm really annoyed with Mathematica and I need your help. I defined a discrete Fourier-Transformation(I use II instead of N, because Mathematica wont let me-.-): Subscript[ϕ, i_] = Sum[Exp[I (2 Pi)/II i k] f[k], {k, II}]; And I want to check that Subscript[ϕ, 1] == Subscript[ϕ, II + 1] This gives me $$\sum _k^{\text{II}} \left(f(k) e^{\frac{2 i \pi k}{\text{II}}}\right)=\sum _k^{\text{II}} \left(f(k) e^{\frac{2 i \pi (\text{II}+1) k}{\text{II}}}\right)$$ And by simply splitting the exponents on the right side of the equations this can be seen to be true.($\exp(i2\pi k)=1\forall k\in\mathbb N$ ) I really would have thought that Mathematica would be able to simplify that on its own, considering that $k$ is the summation index and is therefore an integer, but it does not. So I told Mathematica: Simplify[Subscript[ϕ, 1] == Subscript[ϕ, II + 1], Assumptions -> k ∈ Integers] Didn't help, so I tried: Simplify[Subscript[ϕ, 1] == Subscript[ϕ, II + 1] /. Exp[a_] :> Exp[...

front end - some Graphics output do not fully render on the screen until an extra click is made into the notebook

Sometimes when using a command such as Grid and looking at the output, some of the actual lines (frame, gridlines) that make up the display will be missing on the screen (not finished rendering) After shaking the notebook by holding it with the mouse at the corner and just slightly pulling it to make it little bigger or smaller, the display will now finish rendering and any missing parts of the Graphics will show up. Forcing full rendering can also be achieved by making one extra click of the mouse once anywhere in the notebook. The Graphics can also be made to fully render by moving another application window over the Mathematica notebook and then removing that application window again, all the time without touching the notebook. This might make one think it can be a windows/graphics card/monitor issue? Since it looks like a repaint event is needed by the OS. Using V9, windows 7 64 bit. Intel hardware. ATI Radeon HD 5570 graphics card. (note: On version 8, the same issue is present h...