Hot answers tagged operators
17
Actually, // is not a postfix operator, itself; it would be considered an infix operator, akin to + or -. It operates by turning x // f into f[x]. You can string several of them together, e.g. x // f // g which is equivalent to g[f[x]], and think of them as successive transformations. This can be very useful for crafting complex transformations, but it does ...
14
There is no need to play around with ReplaceAll, Rule, Block, Module or whatever using D, since you have an oparator Derivative really fulfilling your needs while you need not bother if the arguments were defined, so I recommend it to find symbolic derivatives of your function. Remember of shorthands f', f'' to represent first and second derivatives of ...
11
Here is the Mathematica proof. I'll leave out the prefactor $\hbar/i$ for simplicity. Also, in case this is a homework problem, I decided not to add too many comments to the code. Instead I'll let you figure it out. The basic idea is to do cross products and gradients in spherical coordinates. The calculation shown here actually gives you a way to calculate ...
11
A general idea as to how this can be done in a consistent way is explained in the help documents under NonCommutativeMultiply. The thing is that you want to use your operators in an algebraic notation, and that's what that page discusses.
If, on the other hand, you're happy with a more formal Mathematica notation, then you would have the easier task of ...
11
I propose using Interpolation.
list = Prime~Array~3000;
intf = Interpolation[
{list, Range@Length@list}\[Transpose],
InterpolationOrder -> 0
];
Then, for point x:
x = 12225.4;
Which[
x < First@list , {-\[Infinity], First@list},
x > Last@list , {Last@list, \[Infinity]},
True , list[[#-1 ;; #]]& @ intf @ ...
11
While the answer of Szabolcs is clearly the best alternative, if you have already assigned values to the variables and clearing them for some reason is no viable option, you can use
Block[{s, L0, L1, a}, Hold@Evaluate@D[L[s, L0, L1, a], s]]
Unlike Module, Block doesn't introduce new variable names but temporarily removes the values of those given. Hold ...
10
If you need to work with a set of variables symbolically, but you also need to substitute in values for them occasionally, a good approach is to use a rule list:
values = {a -> 0.04, L1 = 1, L0 -> 1}
If the symbols have no values assigned, you can use them normally in symbolic calculations:
L[s_, L0_, L1_, a_] := L1 + L0/(1 + s/a)
D[L[s, L0, L1, ...
9
You can make use of BinCounts. I think this is a very simple to understand solution because BinCounts does almost exactly what you need already.
f[x_, list_List] :=
Module[{bins},
bins = Join[{-Infinity}, Sort[list], {Infinity}];
First@Pick[Partition[bins, 2, 1], BinCounts[{x}, {bins}], 1]
]
But it won't give you two intervals if the number is part ...
9
Something like this
f = D[#, x] + D[#, y] + z # &
seems to work. Use as follows:
f[x ψ[x, y, z]]
to give
$x \psi ^{(0,1,0)}(x,y,z)+x \psi ^{(1,0,0)}(x,y,z)+x z \psi (x,y,z)+\psi (x,y,z)$
9
The problem is that the conversion happens at parsing stage, not evaluation. And after code has been parsed, the details of how function was invoked (prefix, normal way or postfix) are not stored any more, so by the time you evaluate the code, you have no way to tell whether you typed it as f@g@h, f[g[h]], or h // g // f.
In the FrontEnd, you can do ...
7
FullForm prints the expression with no special syntax (e.g. Plus instead of +).
FullForm[a*b]
(* Times[a, b] *)
So you change the expression's head.
Table[fun @@ (a*b),
{fun, {Plus, Subtract, Divide, Dot, Cross}}]
(* {a + b, a - b, a/b, a.b, a\[Cross]b} *)
7
A brute force solution is to check all possible values of this function.
num = {1/10, 1/2, 4/7, 3/5, 2/3};
pow = {0, 1, 2, 3, 4};
To obtain value for one combination use the Inner function
Inner[Power, num, pow, Plus]
(* => 2222701/992250 *)
Then we apply function Inner[Power, num, #, Plus]& on all permutations
prm = Permutations[pow];
val = ...
6
The solutions using Outer and Tuples will all blow up for large lists. The following is a compiled function that exploits the ordered nature of the lists and should be very fast and low on memory footprint even on very large lists.
cf = With[{op = Plus},
Compile[{{list1, _Real, 1}, {list2, _Real, 1}, {n, _Integer}},
Module[{result = ...
6
In case you have any problems, I recommend using the Derivative operator instead of D, since the latter works on expressions, while the former one can work on pure functions.
{Derivative[1, 0][f][x, y], Derivative[0, 1][f][x, y]} // TraditionalForm
The subtlety here is that one cannot use it to find partial derivatives of f at {0,0}, e.g.
...
6
Preamble
I can't resist to answer, although the answer won't be short :-). Below is an attempt to systematically capitalize on properties which weren't used (or at least used systematically) in other answers.
I will use the same lists provided by the OP, for tests:
l1 = {5.7832, 30.4713, 74.887, 139.04, 222.932, 326.563}
l2 = {3.481, 9.2816, 15.7112, ...
5
My favorite:
interval[x_,list_List]:=ReplaceList[Append[Prepend[Sort@list, -Infinity],
Infinity], {___, a_, b_, ___} /; (a <= x <= b) :> {a, b}]
EDIT: Much faster if we eliminate the condition checking as follows:
interval2[x_, list_List] := ReplaceList[#, {___, a_, x, b_, ___} :> {a, b} ] &@
Join[{-Infinity}, ...
5
intervals[x_, list_List] :=
Cases[
Partition[Flatten[{-Infinity, Union[list], Infinity}], 2, 1]
, {l_, u_} /; l <= x <= u
]
Use cases:
In[53]:= intervals[3, Range[10]]
Out[53]= {{2,3},{3,4}}
In[54]:= intervals[3, 2 * Range[10]]
Out[54]= {{2,4}}
intervals[-3, Range[10]]
Out[55]= {{-∞,2}}
In[56]:= intervals[999, Range[10]]
Out[56]= ...
5
Assuming the list list is already ordered, the following should answer your question:
f[x_,list_List]:=
Module[{pos=Last@Ordering@Ordering[Append[list,x]]},
Which[pos==1,{-Infinity,First@list},
pos==Length[list]+1,{Last@list,Infinity},
True,list[[{pos-1,pos}]]]]
5
Lets start step by step. First let us see that your sum is transformed into an build in function
Sum[i^a, {i, 1, r}]
(*
Out[36]= HarmonicNumber[r, -a]
*)
Now we can go two different ways: One way is to derivate this expression wrt r and keep the unknown a. Second is, we substitute a->2 and derive afterwards. If we replace a by the number 2 we get ...
4
There is another way using RankedMin (we don't have to sort the whole list as we would do with Sort, SortBy) :
RankedOrd[l1_, l2_, n_, f_] :=
With[{l = Flatten[Array[{f[l1[[#1]], l2[[#2]]], #1, #2} &, Length /@ {l1, l2}], 1]},
l[[#]] & /@ Table[Position[l, RankedMin[Transpose[l][[1]], i]][[1, 1]], {i, n}]]
for the lists l1, l2 and ...
4
Something like :
f[oper_, list1_, list2_, n_] := Sort[Flatten[
Outer[{oper[list1[[#1]], list2[[#2]]], #1, #2} &,
Range[Length[list1]], Range[Length[list2]]], 1]][[1 ;; n]]
f[Plus, l1, l2, 5]
(* {{9.2642, 1, 1}, {15.0648, 1, 2}, {21.4944, 1, 3}, {33.6058, 1, 4}, {33.9523, 2, 1}} *)
Also :
f2[oper_, list1_, list2_, n_] := Sort[{oper[#[[1, 1]], ...
4
Here are several rules that can help to implement the Einstein convention:
deltaSimplifyRules =
{
(a_ + b_)*x_ :> (a*x + b*x),
KD[i_Symbol , j_]*
x_ /; (MemberQ[Attributes[i], Temporary] &&
MemberQ[x, i, Infinity]) :> (x /. i :> j),
KD[i_ , j_Symbol]*
x_ /; (MemberQ[Attributes[j], Temporary] &&
...
4
Let's demonstrate quite a straightforward approach:
With[{ lst = #1^0 + #2 + #3^2 + #4^3 + #5^4 & @@@ Permutations[{1/10, 1/2, 4/7, 3/5, 2/3}]},
Position[lst, Max @ lst]]
{{12}}
Permutations[{1/10, 1/2, 4/7, 3/5, 2/3}][[12]]
{1/10, 4/7, 2/3, 3/5, 1/2}
#1^0 + #2 + #3^2 + #4^3 + #5^4 & - a pure function of 5 variables, an ...
4
A variation:
mySet = {1/10, 1/2, 4/7, 3/5, 2/3};
perms = Permutations[mySet];
powersums = Total[Transpose @ perms^Range[0, 4]];
Extract[perms, Position[powersums, Max[powersums]]]
{{1/10, 4/7, 2/3, 3/5, 1/2}}
Another variation (beware: slower on large sets):
Last @ Sort @ With[{perms = Permutations[{1/10, 1/2, 4/7, 3/5, 2/3}]},
...
3
Maybe it's this that you are looking for.
Using the notation Package you can write:
I used a image to show these yellow box. To create it, use the notation pallet that Popup when you call the Package.
This is the pallet:
Use the second button to do your new notation.
More information about this in Wolfram help in this link.
3
For evaluating derivatives for numerical arguments, one (somewhat neglected) function for the purpose is SeriesCoefficient[]. Of course, this produces derivatives scaled by factorials, so you have to multiply by the appropriate factorial factors to get the actual value of the derivative. For instance,
Derivative[3, 5][Function[{x, y}, Exp[-1/(x^2 + ...
3
WReach's answer prompted me to write another answer:
intervals[x_?NumericQ, list_List] :=
With[{sl = Sort[Flatten[{-Infinity, list, Infinity}], LessEqual]},
sl[[{#, # + 1}]] & /@
Flatten[Position[
Times @@@ Partition[Sign[x - sl], 2, 1], -1 | 0]]] /;
VectorQ[list, NumericQ]
3
Among many alternatives you can use something like
glblub1[x_, data_List] := Pick[#, IntervalMemberQ[Interval@#, x] & /@ #]& @
( Partition[#, 2, 1]& @
Append[Prepend[Sort@data, -Infinity], Infinity])
or
glblub2[x_, data_List] := Pick[#, (#1 <= x <= #2) & @@@ #]& @
...
3
As it turns out, Combinatorica has the function BinarySearch[] implemented. The code in the package is attributed to Paul Abbott. What follows is a modification of the routine that gives results in the format desired by the OP:
bisect[k_?NumericQ, l_List] :=
Block[{n = Length[l], lo, mid, hi, el},
{lo, hi} = {1, n};
While[lo <= hi,
If[(el = ...
3
The origin of the problem is a small bug appearing in your code here : Derivative[1, 0][HarmonicNumber] (even though this expression evaluates correctly in general, however you should have rather Derivative[1, 0][#1 &][r, 0]! ) for [r,0].
Since we've had :
f[n_, a_] = Sum[i^(a), {i, 1, n}]
HarmonicNumber[n, -a]
This result is a polynomial in n ...
Only top voted, non community-wiki answers of a minimum length are eligible




