# Tag Info

18

Here is my version using injector pattern: ClearAll[myWith]; SetAttributes[myWith,HoldAll]; myWith[pars_=vals_,body_]:= Apply[Set,Hold[Evaluate[Transpose[{pars,vals}]]],{2}]/. Hold[vars_]:>With[vars,body] This code assumes that pars evaluate to a list of symbols. For example, myWith[params=vals,a+b+c+d] (* 10 *)

17

Maybe I miss the point here, but FullForm[x ↗ y] gives UpperRightArrow[x,y]. This is described in the documentation to UpperRightArrow and since this symbol is not protected and has not built-in meaning, you can just define it the way you like: UpperRightArrow[x_, y_] := FooBar[x, y] and this instantly gives you Update: As answer to Jacobs ...

16

It is Kampé de Fériet function, introduced in Joseph Kampé de Fériet, "La fonction hypergéométrique.", Mémorial des sciences mathématiques, Paris, Gauthier-Villars. Its definition is given on Notations page: and, in an alternative form, in Wikipedia: {}^{p+q}f_{r+s}\left( \begin{matrix} a_1,\cdots,a_p\colon b_1,b_1{}';\cdots;b_q,b_q{}'; \\ ...

15

OK, the verbal description isn't very easy, but I'll try: This is a simulation showing how the attractive forces between 21 spheres cause them to aggregate. Instead of simulating the equations of motion, the Dynamic approximates the physics of the attractive force and the repulsive core in the somewhat artificial body of Function[{x}, ...]. Its argument x ...

14

I personally like to use Thread for such things (bounds are e.g. easy to adjust), like: NMaximize[{a + b + c, Thread[{a, b, c} <= {5, 6, 7}]}, {a, b, c}] If it is all the same bound, we can directly write (as in Artes' comment below): NMaximize[{a + b + c, Thread[{a, b, c} <= 5]}, {a, b, c}] I think the syntax should be clear - see also Docu ...

13

# is a placeholder for an expression. If you want to define a function, $y(x)=x^2$, you just could do: f = #^2 & The & "pumps in" the expression into the # sign. That is important for pairing & and # when you have nested functions. f[2] (* 4 *) If you have a function operating on two variables, you could do: f = #1 + #2 & So ...

13

I believe it does work, just not how you expect. :-) From the documentation for PutAppend: Note that there are no quotation marks around filename in the first line. It is not made particularly clear but you can use this syntax with >>>: Range[10] >>> file.txt Which outputs to a file named file.txt directly. This is a special and ...

12

You can use String "keys" for indexed variables, as I did for A combination of Set::setraw and Set::shape errors. The strings can have spaces or any other characters you want to use: var["Degree of the First Polynomial"] = (* stuff *); You also have a wide range of characters, many of which can be used in Symbol names. Go to menu Palettes > Special ...

11

In some languages Print behaves like the identity function. But as noted, Print in Mathematica returns Null. I want to make clear what is going on with the memoization idiom, for those who might be confused by it. I know I didn't really understand it at first. func[y_] := func[y] = Print["Hello world !!!"]; This is clearer if it's written like this: ...

11

The reason can be learned by inputting the following: ?func This outputs the values currently assigned to your function func: Globalfunc func[1]=Null func[2]=Null func[y_]:=func[y]=Print[Hello world !!!] When a function is evaluated that only prints, it assigns it a value of Null which is remembered by the memoization. The documentation covers ...

11

It is of course possible to redefine functions within loops in Mathematica. You are actually just missing a semicolon at the right place for your code to work as intendend: For[i = 1, i <= 5, i++, f[x_] := Sin[x]^2; Print[{i, f[i]}] ] It's probably worth noting (as Jacob did in his comment) that the semicolon is just a shortcut for a ...

11

The problem is just name collisions, that isn't at all abstract and will happen in any programing language, so it would be odd to claim that it's impossible due to the way Mathematica works. The solution is simply to name your parameters when you write your functions so they don't collide, so you write for instance: RegionFunction -> Function[{a1, b1}, ...

11

I can't answer how the association is made for the built-in operators, but I can show how to add your own. If your symbol is already an operator you can do this simply as halirutan showed. This question may be a duplicate of How can one define an infix operator with an arbitrary unicode character? but since it admits a simpler interpretation I shall not ...

10

This is perhaps a place to start: position[expr_, level_: 1] := With[{positionData = SortBy[ #[[1, 1]] -> #[[All, 2]] & /@ GatherBy[Extract[expr, #, Verbatim] -> # & /@ Position[expr, _, level], First], Min[Length /@ #[[2]]] & ] // Dispatch}, Replace[#, positionData] & ] The second argument controls the ...

10

How about: Export["t.txt", Table[j + 10 i, {i, 0, 9}, {j, 0, 9}], "List", "LineSeparators" -> ";\n"] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; {20, 21, 22, 23, 24, 25, 26, 27, 28, 29}; {30, 31, 32, 33, 34, 35, 36, 37, 38, 39}; {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}; {50, 51, 52, 53, 54, 55, 56, 57, 58, 59}; {60, ...

9

I think you only forgot a comma. Try: For[i = 1, i <= 5, i++, {f[x_] := Sin[x]^2, Print[{i, f[i]}]}] this gives your desired output. If I were you, I would not define a function in a For Loop (can be time consuming). And, if possible, I would work with a Table because this works faster too. So do something like: f[x_] := Sin[x]^2; Table[{i, f[i]}, ...

9

I appreciate very much, that you wrote up such a nicely formatted question, although this is your first post. Therefore, let's put the comments into an answer. Your first issue was that you used ( ) where you should have used [ ]. That's maybe not obvious for starters and I have seen this mistake very often. There are different types of braces and it's ...

9

Sequence means more or less "no head". What you want to do is to remove the head List from an inner list. Or, put in another way, you want to replace this head with "no head". The operation that changes one head to another is Apply. Therefore, what you really want is f[a, b, c, d, Sequence @@ array] where @@ stands for Apply.

8

While the tutorial will undoubtedly explain better that I could the entire topic of pure functions, which is what Slot, or # has to do with, I'll answer the specific question at hand. The Slot is treated as the argument in an anonymous function. Specifically, the code #^2 & // FullForm Reveals that what is actually going on is ...

8

Your python idiom can be implemented in Mathematica using Part and Span as: Range[10][[-1;;1;;-1]] (* {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} *) which is very similar to your python command, and doesn't require you to unprotect either Span or Part.

8

Borrowing from Mr. Wizard's answer to a related question, I came up with the following. ClearAll[myWith]; SetAttributes[myWith, HoldAll]; myWith[vars_ = init_, expr_] := Function[Null, With[{##}, expr], HoldAll] @@ (Thread[Hold[Set][Hold[vars], Hold[init]] /. {x__} :> x, Hold] /. Hold[Set] -> Set) Examples Clear[a, b, c, d]; a = 10; myWith[{a, ...

8

The Notation package is the most convenient way to define new notation(s). <<Notation Define an infix notation. You can use the palette that the 'Notation package pops up to do this. InfixNotation[ParsedBoxWrapper["\[UpperRightArrow]"], FooBar] Check that the infix notation maps to the correct FullForm expression. x \[UpperRightArrow] y // ...

8

You could use the Notation package: << Notation Symbolize[ParsedBoxWrapper[FractionBox["d\[Sigma]", "d\[CapitalOmega]"]]] (d\[Sigma]/d\[CapitalOmega])[\[CapitalEpsilon]_, r_, \[Theta]_] := f[\[CapitalEpsilon], r, \[Theta]]

7

To answer your question: In this case you could just type a fake Span call and keep it unevaluated. When you then look at the fullForm you see: Hold[list[[ ;; ;;-1]]]//FullForm (* Hold[Part[list,Span[1,All,-1]]] *) what you actually called: Span[1,All,-1]. Since you now know what happens, you can catch, when someone calls Part[list_, Span[1, All, -1] ...

7

It seems to me that your tracing gives the answer to your question. The output of the last three traces show identical evaluation and, indeed, this is true in general -- @ is the prefix form of [] and // is the postfix form. The first trace, the one using /@ (which is just the infix form of Map) yields the same result as the other three, but arrives at it ...

7

Here's another way to proceed, using Derivative[], and sidestepping the use of a dummy variable: LogDerivative[f_] := Derivative[1][Composition[Log, f]] Test: LogDerivative[Sin][x] Cot[x] LogDerivative[Gamma][x] PolyGamma[0, x] LogDerivative[#^3 &][x] 3/x

7

If I understand the question here are three ways to "nest" functions: f1 = Function[x, (# + x)/2 &]; f2 = With[{x = #}, (# + x)/2 &] &; f3 = # /. x_ :> ((# + x)/2 &) &; All work the same: #@7 & /@ {f1, f2, f3} {(#1 + 7)/2 &, (#1 + 7)/2 &, (#1 + 7)/2 &} Note that with the first form I used the Slot based ...

7

If you do these things a lot you may consider building your own syntax to be able to write constraints in a more concise manner, e.g.: constrAnd[list_, func_] := And @@ (func /@ list) lt[list_,n_] := constrAnd[{a, b, c}, # <= n &] lt[{a, b, c},5] a <= 5 && b <= 5 && c <= 5 So that you may now write NMaximize[{a + b + ...

7

I can see the source of your confusion: If you use Head[f[x]] and Head[5] you get f and Integer respectively. Then, you read the documentation Apply[f,expr] or f@@expr replaces the head of expr by f. and you expect Cos@@5 to replace the Integer head by Cos. The way I explain it to myself is by saying Mathematica has two (types of) heads ;-) One type is ...

7

They return different answers because they're actually different expressions that have the same value, but of course that equality isn't going to be preserved when working with inexact numbers like 4.3. In particular, the first four expressions are all interpreted by Mathematica as Sqrt[(-4.3)^2] while the last expression is interpreted as Sqrt[-4.3]^2 ...

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