# Tag Info

## Hot answers tagged function-construction

9

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice: Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column Test: f[1, 2, ...

7

reg = BoundaryDiscretizeGraphics@ ParametricPlot[ BSplineFunction[{{0, 0}, {1, 0}, {2, .5}, {1, 1}, {0, 1}}, SplineClosed -> True][t], {t, 0, 1}] Plot3D[Sin[6 x y], {x, y} ∈ reg]

6

There are many ways to achieve this, I'd probably do something simple like f[] = f[{}] (*set or set delayed, depends of context*) But you can also do something more fancy, although it is not compact enough for me to like it :P Default[f] = {}; f[Optional@{x_: 1, y_: 2}] := {x, y} f[] f[{}] {1, 2} {1, 2}

6

Without a more complete example of your function all I can offer are bare guidelines to (hopefully) point you in the right direction. The first level is merely syntactical; you would use OptionsPattern, OptionValue etc., in a high level function simply for convenience, but pass all arguments as machine types to an inner compiled function. A second level is ...

6

It was actually a lot easier than I thought f = Function @@ {vars, expr}

5

If you have a list of variables stored in vars in the desired order and expr is the function's formula, then f = Function @@ {vars, expr} will define a function for you. If you're content with the variables being in their sorted order and the expression expr is polynomial-ish (test Variables[expr] first), then f = Function @@ {Variables[expr], expr} ...

5

Let's start with slightly modified version of mergeRules, that takes into account fact that options can have symbolic or string names and name -> val is treated the same as "name" -> val: ClearAll[symbolToName, deleteOptionDuplicates] symbolToName[sym_Symbol] := SymbolName[sym] symbolToName[arg_] := arg deleteOptionDuplicates[opts:OptionsPattern[]] ...

4

Preamble There are some important differences (or, more precisely, features of Function which can't be reproduced with symbols and rules), that have not been reflected in answers here, but that I think deserve a separate answer. These are related to some more advanced uses, involving evaluation control, closures, and garbage collection. Emulating Hold ...

2

Sorry for being unprecise. I'm just wondering if your solution is the "official" way to deal with optional list arguments like in ImageTake. I also tried f[args:{x_, y_}:{1,2}] := {x, y}which seems to work. However, I'm not sure if understand it the right way... I do not believe I have seen a guideline or even a standard practice for this particular ...

2

The question deals with a particular operator characterized by the commutation relation $[a, a^\dagger] = 1$. The polynomial to be expanded is also of a special form, $(a+a^\dagger)^n$. Also, the whole polynomial is supposed to be applied to the unique state vector $\vert 0\rangle$ that has the property $a \vert 0\rangle = 0$ (which is the null vector). So ...

2

This approach seems to work. Clear@f list = {a, b, c} argList := Evaluate@ Sequence @@ (Pattern[#, Blank[]] & /@ list) f[argList] := a + b + c DownValues@f (* {HoldPattern[f[a_, b_, c_]] :> a + b + c} *) f[1, 2, 3] (* 6 *) I think it fits your request. If not, maybe try manipulating DownValues[f] directly, but it won't be elegant! Edit: ...

2

You can still use the old functions from Combinatorica, either to tide you over while you figure out the new ones, or as a semi-permanent solution. WRI has also provided a handy guide to transitioning from that package to the new built-in functions: "Upgrading from Combinatorica" For example, in the case of the obsolete code you mentioned: << ...

2

Walk through this a step at a time with each line of input followed by the line of (* output *) a = 17; b = 697; F = FactorInteger[a*(27*b^2 + a^6)] (* {{2, 2}, {13, 1}, {17, 3}, {37, 1}, {67, 1}} *) v = Map[First[#]^Last[#] &, F] (* {4, 13, 4913, 37, 67} *) (* When it returns {ReplaceAll[x],p} there was no solution *) {x/.ToRules[Reduce[{(x+a)^3==-b, ...

2

I think there is another way to do that: f[s : (PatternSequence[_?NumericQ] ..)] /; Length[{s}] == 3 := Function[{a, b, c}, a^2 Sin[b] Log[c]][s] It's suitable for the cases that conditions are the same.

1

Supposing that you want a DownValues definition rather than a Function you could use: (* with your expression assigned to expr *) (x \[Function] x_) /@ Variables[expr]; f[Sequence @@ %] = expr;

1

f[ω0_, α00_, α01_, α02_, α10_, α11_, α12_, α20_, α21_, α22_, β01_, β02_,β11_, β12_, β21_, β22_] := ... where ... is the list in your OP. N.B. you're missing ω1 as an argument, I assume that's intentional...

1

This seems to accomplish what you wanted, but it's not returning your result. I have commented the code a bit more heavily than usual to show how I put it together and what it accomplishes: hopefully this may be helpful to you in constructing similar functions in the future. function[a_, b_] := Module[ {factorlist, listofsolutions, toCR}, (* Generate ...

1

Yes, for this you can use Map (as operator /@) which is one of the most often used functions when doing the same job for several inputs. For instance creating 3 data sets {g1, g2, g3} = Range /@ {10, 20, 30} and then creating your interpolations {i1, i2, i3} = Interpolation[#, InterpolationOrder -> 1] & /@ {g1, g2, g3} If you don't know what the ...

1

Code outline: ClearAll[XMLNote]; (*modified, only act on target tags*) XMLNote[ XMLElement[tag : "section" | "TextHeading", attributes_, data_], m_Integer ] := ... (*new, for other tags pass down XML elements*) XMLNote[XMLElement[_, attributes_, data : {__XMLElement}], m_Integer] := Sequence @@ (XMLNote[#1, m + 30] &) /@ data; ...

1

Calculations about equality There are different kinds of "equality" used in computer programming. In Mathematica, ==, or Equal is used to assert equality. 1==1 is a True statement, and 1==2 is False. f == a/b is a statement about the relationship between f, a and b. You shouldn't use := to assert equality between f and a/b or c/d. Rather, make the ...

1

Not sure if I've understand you. Something like this?: With[{a = a[c], b = b[d]}, D[f[a, b], c]] With[{a = a[c], b = b[d]}, Solve[f == f[a, b], c]] 1/d {{c -> d f}}

1

Assuming $G=\rho=1$, move the gradient operator into the integral:  \begin{align} - \mathbf{g} = \nabla V(X,Y,Z) &= \nabla \int_{-D}^{D} \int_{-B}^{B} \int_{-L}^{L} \frac{{dx}'{dy}'{dz}'}{\sqrt{(X-{x}')^2+(Y-{y}')^2+(Z-{z}')^2)}} \\ &= \int_{-D}^{D} \int_{-B}^{B} \int_{-L}^{L} \nabla ...

1

Simplest solution Use Set instead of SetDelayed: vl = {a_, m_, s_, τ_, c_, x$HH_, x$HF_, x$FF_, x$FH_, λ$H_, λ$F_, L$H_, L$F_, w_, δ_}; vle = {a, m, s, τ, c, x$HH, x$HF, x$FF, x$FH, λ$H, λ$F, L$H, L$F, w, δ}; vle1 = {1, 1, 1, 1, 1, 1, 1, x$FF, x$FH, λ$H, λ$F, L$H, L$F, w, 1}; xx[vl] = vle; xx[vle1] xx[2 vle1] (* {1, 1, 1, 1, 1, 1, 1, x$FF, x$FH, λ$H, λ$F, ...

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