# Tag Info

2

f[x_] := x^2 f[x_, n_] := Nest[f[#] &, x, n] f[2] f[2, 3] (* 4 256 *)

4

Here is a more generalized method for what you want to do: Let's define our momentum operator as you did above: P := -I * h * D[#, x]& Then we can define the nth power operator in a more general way as: T[n_] := Nest[P, #, n] & So for example the Kinetic energy operator (which is P^2 / (2 m)) will be: T[2] / (2 m) And we can use it on some ...

3

You can't just squre an operator since no multiplication of functions (operators) are defined in Mathematica. You have to write something like T = (p@p@#)/(2 m) & or T = p[p[#]]/(2 m) &

2

EDIT As per advice from Kuba, my answer was terse. Also see here re: an instructive use of DynamicWrapper by Kuba. Here is a toy example for illustrative purposes: data = {{{1, 2, 3} -> "apple", {2, 3, 4} -> "banana"} -> "fruit", {{3, 4, 5} -> "celery"} -> "vegetable"}; va = ""; co["fruit"] := Red; co["vegetable"] := Yellow; g[___, ...

2

Your syntax is not quite right, which is why your attempt did not work. Pattern objects should not be on the right-hand-side of the definition. Correcting that alone solves the problem: f1[x_, y_] = {{x, y}, {x y, x + y}}; f2[x_, y_] = {{x, y, 0}, {0, x + y, 0}, {1, x - y, x y}}; g[1, x_, y_] = f1[x, y]; g[2, x_, y_] = f2[x, y]; Test: g[2, a, b] ...

1

g[1, x_, y_] := f1[x, y] g[2, x_, y_] := f2[x, y] It works.

2

Another approach is to define your f's in a manner more conducive to easy manipulations down the road. For instance, f[1, x_, y_] := {{x, y}, {x y, x + y}}; f[2, x_, y_] := {{x, y, 0}, {0, x + y, 0}, {1, x - y, x y}}; g[i_, x_, y_] := f[i, x, y]; allows a simple definition of g. Of course, in this case, you don't even need the g at all.

8

ArrayFlatten[Outer[Times, mat, Rmat]]

5

Try this: ArrayFlatten[Map[Rmat*# &, mat, {2}]]

2

This is really easy if you understand the internal form of {a,b,c,d}. Let's look at it: p={a,b,c,d}; FullForm[p] (* List[a,b,c,d] *) as you see what you want is not really far away because basically, you only need to replace List with f. This is exactly what Apply (or as operator @@) does: f @@ p (* f[a, b, c, d] *)

1

You question is not very clear. a = {p[1], p[2], p[3]} f[x__] := 2 x f[a] (* or *) f /@ a (* or *) Thread[f[a]] yields, e.g. {2 p[1], 2 p[2], 2 p[3]} Note also I used a for the vector, else recursion... If you just want to transmute the vector into an argument list: f[Sequence @@ p]

2

In these situations we use pattern matching instead of conditionals in Mathematica. You just need to make two definitions: f[x_?NumericQ, y_?NumericQ, s_] := ... f[x_, y_, z_] := ... The first, more specific one will be used if both of the first two arguments are numeric. The second one will be used otherwise. Personally I consider doing this very bad ...

5

I took a rather shotgun approach to the question and got a range of behaviors. The range is rather confusing, so I agree with Szabolcs's conclusion that using Hold this way is not supported. First, NMinimize[Hold[Print["hi"]; x^2], x] prints "hi" and then crashes the kernel, while NMinimize[Print["hi"]; x^2, x] prints "hi" and returns {0., {x -> 0.}}. ...

9

I don't know why no one mentioned this, but all you have to do is to use a special form of OptionsPattern: pfunc[x0_, plotopts : OptionsPattern[{Plot, pfunc}]] := your-code where inside OptionsPattern go all sub-functions you need, in a list. Now everything is fine and dandy. There might be a downside of this, in case when several sub-functions can take ...

9

It gives those errors because you explicitly specified that pfunc only has "test" as an option. OptionValue is finicky and will complain if it sees options that it doesn't recognize. There are a couple of alternatives that I can think of: 1: Use FilterRules everywhere instead of OptionValue ClearAll@pfunc2 pfunc2[x0_, plotopts : OptionsPattern[]] := ...

5

I believe you need to add all of Plot's options to pfunc as well, like this: Options[pfunc] = Join[ Options[Plot], {"test" -> True, ...} ] I'd like to note that this is what builtins do as well. For example, Plot also carries all possible Graphics options. The downside is that any changes to the default options of Plot won't affect pfunc. The upside ...

0

I have found a workaround, but I don't think it's the most elegant solution available. Basically, I separate the Plot options from the pfunc options via curly brackets, like this: Options[pfunc] = {"test" -> True}; pfunc[{x0_, plotopts : OptionsPattern[]}, OptionsPattern[]] := Module[{}, Plot[x^2, {x, -x0, x0}, PlotStyle -> ...

2

Try something like: SetDirectory["whatever"]; Fold[ImageMultiply[#1, #2]&, #[[1]], Rest@##] &@ (Import/@ FileNames["*.gif"])

8

The simplest solution is to rescale the data. Suppose we have a distance limit of $r_x = 2$ and $r_y = 1$ and a point set data = {{x1,y1}, {x2,y2}, ...} Instead of working with data and these two radii, work with a single radius $r=1$ and the dataset dataScaled = {{0.5 x1, y1}, {0.5 x2, y2}, ...} Finally, transform the results back to the original ...

0

The DistanceFunction seems the way to go. However you may also want to rescale your dataset so that the ellipse constraint becomes a circle constraint.

3

DistanceFunction is one way to go. For example, the code below generates a random set of points in the box with all elements drawn from -1 to 1 and then selects the 20 points that are closest to the point {0.5, 0} (in the sense of an ellipse defined by the matrix a). In the example, a is tall and skinny, but could have any orientation and scale. data = ...

4

I guess one method would be to take the larger radius of your ellipse to collect all possible candidates with Nearest[data,x,{n,r}] and after that filter them with e.g. Select to find all points that are inside your ellipse.

0

Make the positive number sign a space and switch on SignPadding NumberForm[#, {6, 4}, NumberPadding -> {"0", "0"}, NumberSigns -> {"-", " "}, SignPadding -> True] & /@ {9.0001, 10.0001, -8.0001} (* { 09.0001, 10.0001,-08.0001} *)

1

I think Mathematica is padding one extra position on the left to preserve space for a possible minus sign. NumberForm[#, {6, 4}, NumberPadding -> {"*", "0"}] & /@ {1234.567, -1234.567, 12.34567, -12.34567, 1.234567, -1.234567} {*1234.5700, -1234.5700, *12.3457, -12.3457, **1.2346, *-1.2346}

2

Having f[x_] := x^1.1 - 2.5 x^.5 And also knowing that the formula of the tangent line is f'[x](x-a) + f[a] You could just make a Plot with it. With a=1, as you requested: Here is the code of it: With[ {a = 1}, Plot[ { f[x], f'[a] (x - a) + f[a] }, {x, 0, 10}, PlotRange -> {-4, 4}, PlotStyle -> Thick, Epilog ...

0

I would write it like this, which I find quite readable: onlyvarsQ[expr_, vars__] := Cases[expr, Except[_?NumericQ | vars], {-1}] == {} e.g. expr = (1 - Exp[I*(x - 5.6)*23.4])*Log[Abs[x - 4.3]]/(1.7 + 3.2*I - x^2); onlyvarsQ[expr, x] (* True *) onlyvarsQ[y + expr, x] (* False *)

1

Is this what you want? ClearAll@ExactVarsQ; ExactVarsQ[func_, vars__] := MatchQ[ Sort@DeleteDuplicates@Cases[func, x_ /; (AtomQ[x] && ! NumericQ[x]), {0, Infinity}], Sort@DeleteDuplicates@{vars} ] test1 = (1 - Exp[I*(x - 5.6)*23.4])*Log[Abs[x - 4.3]]/(1.7 + 3.2*I - x^2) test2 = (1 - Exp[I*(x - y)*23.4])*Log[Abs[x - 4.3]]/(1.7 + 3.2*I - ...

4

You said you tried using even[ff, x_] := (ff[x] + ff[-x])/2 but I guess you forgot to put the underscore on the first argument. If you do even[ff_, x_] := (ff[x] + ff[-x])/2 instead, then it works. g[x_] := x + x^2; even[g, x] x^2 P.S. No SetAttributes necessary using this method.

8

SetAttributes[{even, odd}, HoldAll]; even[f_[x_]] := (f[x] + f[-x])/2; odd[f_[x_]] := (f[x] - f[-x])/2; Usage g[x_] := x + x^2; even[g[x]] x^2 OR as Szabolcs suggested using pure functions: even[f_] := (f[#] + f[-#])/2 &; odd[f_] := (f[#] - f[-#])/2 & Usage Using the same g as above even[g][x] x^2

4

Given that the word "quickly" is in your title and that you use NIntegrate in your example, you might try the following, purely numerical approach to your problem. findMedian[f_, {x_, a_, b_}] := Module[ {y, interpolatingFunction}, interpolatingFunction = y /. First[ NDSolve[{y'[x] == f, y[a] == 0}, y, {x, a, b} ]]; ...

10

When there is no built-in function, it is often straightforward to build one. Here's a way to calculate the median value of an integrable function: f[x_] := x^2; a = 1; c = 3; FindInstance[Integrate[f[x], {x, a, b}] == Integrate[f[x], {x, b, c}], b] // N {{b -> 2.41014}} You already have a formula for the mean: Integrate[f[x], {x, a, c}]/(c - a) 13/3 ...

3

Simplify[c == y - x^3, Assumptions -> x^3 - 3 + c == y] False Simplify[c == y - x^3 + 3, Assumptions -> x^3 - 3 + c == y] True

4

MeshFunctions is useful for discrete changes in color in Plot: Show[ Plot[2.9*Tanh[5 x] + 0.3, {x, -2.5, 2.5}, PlotRange -> {-3, 3.5}, Axes -> False, Frame -> True, FrameTicks -> None, PlotStyle -> {Blue, Thick}], Plot[-x^3 + 3.5*x + 0.5, {x, -2.5, 2.5}, Mesh -> {{-1, 1}}, MeshShading -> {Darker@Green, Black}, PlotStyle -> ...

2

As b.gatessucks writes in his comment, you want make two plots and combine them with Show. Also you need to modify your color function a little. p1 = Plot[-x^3 + 3.5*x + 0.5, {x, -2.5, 2.5}, PlotRange -> {-3, 3.5}, Axes -> False, Frame -> True, FrameTicks -> None, PlotStyle -> Thick, ColorFunction -> (If[Abs[#] > 1, ...

0

A combination of Hold and Evaluate can achieve this: Hold[mytest[Evaluate[...],...]]. Illustrated with the example given in the question: parameters = {a -> 1} mytest[expr_, parameters_] := Module[{}, (* globpar = 2; *) NIntegrate[expr*a /. parameters, {var, 0, 1}] ] mytest1[parameters_] := Module[{}, globpar = 1; NIntegrate[ ...

2

Please examine this and determine if it is giving the result that you desire: m = 3; mem : mult[i_, j_] := mem = (tstar[Abs[i - j]] + tstar[i + j - 2])/2 ptab = Table[Expand @ Sum[a[i - 1]*mult[i, j], {i, m}], {j, m}] Table[Coefficient[j, tstar[i - 1], 1], {j, ptab}, {i, m}] // Transpose If you include the definitions of a and tstar I may be able to ...

Top 50 recent answers are included