# Tag Info

2

It sounds like you are describing pattern guards or pattern-matching syntax found variously in ML/SML/OCAML/Haskell/F#. For example, in Haskell one could write: -- This is Haskell, *not* Mathematica f a x | a == 1 = x ^ 2 | a == 2 = x * 12 Mathematica does not support this kind of syntax using a pipe symbol. The definitions exhibited in the ...

3

You may be remembering a way that I sometimes write function definitions or replacement rules using what I call vanishing patterns, using Alternatives (short form |). Some examples: Simple power counting Function argument to default under certain condition How to get Cases to wrap results in an arbitrary head? This technique can considerably condense ...

2

There is a Switch statement, but the syntax to do what you need is f[s_,x_]:= Switch[s, 1, x^2, 2, x*12, True, "something else" ] The third option *which I forgot in my first edit) takes care of what happens when s is neither 1 or 2. Otherwise you could use the functional overloading approach you mentioned in your question. It works because ...

1

Do you mean this? f[func_, {Min_, Max_}] :=Manipulate[ Column[{a,Plot[func[x], {x, Min, Max}, Epilog -> {Red, PointSize[Large], Point[{a, func[a]}], Green, PointSize[Large], Point[{0, func[a]}]}]}, Alignment -> Center], {a, Min, Max}] f[Sin[#] &, {0, 2 Pi}]

1

Couple of ways, e.g.: ClearAll[h, f] h = 2 s + 3 s^2 + Sin[s]; f[sarg_] := h /. (s -> sarg) f[2] (* just define directly *) ClearAll[h, f] f[s_] := 2 s + 3 s^2 + Sin[s] f[2] (* 16 + Sin[2] 16 + Sin[2] *)

1

Problem is that "C" is protect symbol. If chance "C" for exampte to "Cu" it works. Cu[n_] := Sum[c[i], {i, 1, n}]

5

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) &

1

<< DiscreteMathCombinatorica; g[f_,n_,s_]:=Plus@@Apply[f,PadRight[TransposePartition[#],s]&/@Partitions[n,s],1]; should even work in version 4.0

4

IsMyPattern[expr_] := MatchQ[expr, F1[x1_]*F2[y1_] + F1[x2_]*F2[y2_] + F1[x3_]*F2[y3_] /; (x1 =!= x2 && x1 === x3 && y1 =!= y2 && y2 =!= y3)] Row[{ v = F1[a]*F2[b] + F1[c]*F2[d] + F1[a]*F2[e]; IsMyPattern[v] (*True*), w = F1[a]*F2[a] + F1[c]*F2[c] + F1[a]*F2[e]; IsMyPattern[w] (*True*), x = F1[a]*F2[b] + F1[a]*F2[d] + ...

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.

3

The function g you describe can be implemented in a simple way like this: g[n_, s_] := Total[Multinomial @@@ IntegerPartitions[n, {s}]] I'll admit I didn't go through your code. Is the above helpful?

8

ArrayFlatten[Outer[Times, mat, Rmat]]

5

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

7

E.g. using Cases: Cases[expr, _F, Infinity] {F[0, 0], F[0, 1], F[2, 0]} Note that the 3rd argument is the levelspec. See e.g. expr//FullForm why it's needed EDIT (I wasn't careful!) Note that this does not work for expr = F[0,0] as by default, Cases does not match the whole expression (it starts at level 1). If that could be the case, you can ...

1

If you want n! terms, this is the thing: expr[n_] := Sum[c[[i, 2]] Derivative[Sequence @@ Table[c[[i, 1, j]], {j, n}]][f][##], {i, n!}] & @@ ToExpression["z" <> ToString[#] & /@ Range[n]] I'm not sure, but with 24 for n=4, I'm kinda convinced, even though you have 3 terms for n=2.

3

The biggest issue in trying to put this together is that NProbability mixed with EmpiricalDistribution doesn't seem to like array indexed variables. Building up symbols programatically seems to fix the issue. edfP[data_?MatrixQ][t__?NumericQ] /; Length[{t}] == Length[data[[1]]] := Block[{vars, x}, vars = Table[Symbol["x" <> ToString[i]], {i, ...

1

One workaround is to define g as: g[aa_] := Block[{a = aa}, NIntegrate[f[x], {x, 0, 1}]] This works because f[x] now is evaluated in local environment where variable a has a value. The reason why the version g[a_] := NIntegrate[f[x], {x, 0, 1}] does not work is that a is absent on the right side of the definition, so nothing will be replaced: ...

1

Use pure functions as a return value : fOne[x_] :=(*just return a piecewise*) Piecewise[{{#^2, # < x}, {#^2 + (x - #)^3 Sin[3 #], # > x}}] & fTwo[y_, z_, w_] := Module[{vars}, Plot[fOne[y][x], {x, z, w}]] fTwo[3, -3, 8]

0

You are having a scoping problem here, because inside a Module your variable x will be renamed internally. Just replace your temporary definition of f with a ReplaceAll. I don't have your definitions for energies, effmass, etc, so I'm not 100% sure but normally this should work: getinter[a_, b_, u0_, k_, m_, hbar_, Nu_, Np_, up_] := Module[{ekp, ms, ...

6

I like to use properties like those in SparseArray and I find subvalues very useful for defining and accessing them. This is best used with a dummy head. The following is some code pulled out from one of my packages and modified. I've defined func here to be a minimal example of what your actual function might look like. Clear[func, myHead] func[str_] := ...

1

J[f_] := Transpose[Map[Function[j, j[##]],Map[Apply[Derivative[##][f] &, #] &, IdentityMatrix[Length[{##}]]]]] & J[{#1 #2 + #1^2 - 2, Cos[#1 - #2] Sin[#1] - #2} &][1.5, 2.5] (* {{5.5, 1.5}, {0.877583, -1.83936}} *) Or in more readable form J[f_] := Module[{n, j}, n = Length[{##}]; j = Map[Apply[Derivative[##][f] &, #] &, ...

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

5

It doesn't work because it's simply not correct syntax. String patterns and expression patterns are not interchangeable. Each works only with its own set of functions: string patterns work only in StringMatchQ and expression patterns only work in MatchQ. In function definitions you can only use expression patterns. You can use something like this ...

3

Short answer: You need to set the attribute HoldAll on your function to prevent the variable to be modified from being changed into its value before it's substituted into the assignment. See long answer below. AppendTo has this attribute (see Attributes[AppendTo]). In other languages, pass by reference is used for two purposes: avoiding copying large ...

Top 50 recent answers are included