Tag Info

Hot answers tagged

19

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 ...


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


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{}'; \\ ...


14

Try this: SetAttributes[createPrimitive, HoldAll] createPrimitive[patt_, expr_] := Typeset`MakeBoxes[p : patt, fmt_, Graphics] := Typeset`MakeBoxes[Interpretation[expr, p], fmt, Graphics] Example: createPrimitive[face[x_: 0.1], {Circle[{0, 0}, 1], Circle[{-0.3, 0.5}, x], Circle[{0.3, 0.5}, x], Line[{{-0.4, -0.2}, {0.4, -0.2}}]}] It works as ...


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 ...


14

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 ...


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 ...


13

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

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}, ...


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, ...


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

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 // ...


10

First off, it's apparent that k needs to take some (positive integer) value, since it's the end-value of the iterator later on. So I add: k = 15; The next bit of the code sets up a recursive function where inttstar[i] depends on periods i-1 and i+1. (This looks a lot like some economic model to be solved.) Notice the inttstar[i_]:= inttstar[i] = (* etc *) ...


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]}, ...


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

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]]


8

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 ...


8

CirclePlus is a built-in symbol already with no meaning for the kernel, but meaning in the front-end. The second definition tried to use the first definition (with head 'Function'), which is protected. (Note the pattern [a_,b_] appearing in the error message, which tells you the left-hand-side is the issue.) Just one line is enough as @rm-rf said, like ...


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

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 ...


7

The two definitions are not the same... it changes what kind of definition is associated with the symbols. Consider the following: Syntax 1: ClearAll@Foo Foo[x_, y_] := Format[MatrixForm[{{x},{y}}]] DownValues@Foo FormatValues@Foo (* {} *) Syntax 2: ClearAll@Bar Format[Bar[x_, y_]] := MatrixForm[{{x}, {y}}] DownValues@Bar (* {} *) FormatValues@Bar ...


7

This is in fact tricky. But j is not what it looks. TensorRank[i] gives 2 and its dimensions are {3,1}. j is different: TensorRank[j] gives 1 and its dimensions are {3} instead of {3,1}. A fix. j = {{1,2,3}} and you get i.j {{1, 2, 3}, {2, 4, 6}, {3, 6, 9}} j.i gives {{14}}. The reason it apparently works with j.i is that in this case ...


6

Good question. I see this was largely answered in the comments yesterday, but since no one posted a formal answer I shall. Cases 2, 3, and 4 appear relatively straightforward. (Incidentally you should be using :>, RuleDelayed here, rather than ->, to localize the pattern names x and y.) The first case that swaps positions needs a closer look ...


6

Your operator must depend on both function and variable - in analogy to D function: logD[f_, x_] := D[f, x]/f or an alternative definition: logD[f_, x_] := D[Log[f], x] Of course your variables of differentiation and in the function must agree. Test it: logD[f[x], x] Derivative[1][f][x]/f[x] logD[Sin[x], x] Cot[x] f = x^2; logD[f, x] ...


6

Many functions, such as the one you've defined f[x_]:=x^2 are automatically listable. This means that you can get a list of the values directly using f[Range[10]] to get the f applied to the first 10 integer values. To get the form you desire, a listing of {x_value, f[x_value]} pairs can be constructed straightforwardly as Transpose[{Range[10], ...


6

What about this (because NMinimize also accepts a list of boundary conditions): NMaximize[{a + b + c, # <= 5 & /@ {a, b, c}}, {a, b, c}] or (if you are after the very same expression) NMaximize[{a + b + c, And @@ (# <= 5 & /@ {a, b, c})}, {a, b, c}] Both are obviously not more compact as such, but very easily adapted to larger number of ...


6

Sorry, I sometimes write rather opaque code in my own quest for brevity and a certain style. You seem to have a pretty good understanding of my code, though I'm sorry it took as much effort as it did. I believe in Mathematica documentation the term "pure function" is used in the manner than "anonymous function" is used elsewhere. I agree this is confusing ...



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