New answers tagged functions
1
I think I understand what your trouble might be. Here is your code:
a := b means that the left-hand-side is a pattern and the right-hand-side should be evaluated when that pattern is found (roughly speaking). The right-hand-side in this case is:
Your function in essence returns the entire module. You can see this by changing Module to something else, ...
3
Print returns Null, and you're returning a list of values, some of which are also Null. So:
ReverseList[ele_List] :=
Module[{list = List[], i, k = 1},
For[
i = Length[ele], i > 0, {i--, k++},
{AppendTo[list, ele[[i]]]}];
Print[list];
list]
would be slightly better, perhaps?
(Obviously you wouldn't really reverse a list ...
2
Here's an even simpler way to create a "list" of functions, just define the function to have two arguments. To follow your example:
f[i_, t_] := t i^2
So now if you want the 5th function, it's
f[5,t]
which gives 25 t. Or you can evaluate it at any point:
f[5,7]
to get 175. If you wanted f to behave more like a proper list so as to accept only ...
1
Not sure exactly what you're looking for, but perhaps one of these steps will help.
(1) Create a list of functions you wish to apply:
fcnlist = {Sin, Cos, Tan}
(2) Now define myfunc using a pure function which applies the selected function from fcnlist to an argument x of your choice:
myfunc[f_, x_] := fcnlist[[#1]] /@ {#2} &[f, x]
(3) To generate ...
4
Mathematica defines Binomial for non-integer inputs as follows:
$$
\binom{n}{m} = \frac{\Gamma(n+1)}{\Gamma(m+1)\Gamma(n-m+1)}
$$
You'll find this under the Details section on the documentation page of Binomial.
3
Something like this?
functionList1 = Table[Evaluate[#*i*i] &, {i, 0, 4}]
functionList2 = Function[x, x^2 # &] /@ Range[0, 4]
functionList1[[3]]@10
functionList2[[3]]@10
(*
{0 &, #1 &, 4 #1 &, 9 #1 &, 16 #1 &}
{0^2 #1 &, 1^2 #1 &, 2^2 #1 &, 3^2 #1 &, 4^2 #1 &}
40
40
*)
3
Perhaps this is what you want?
functionList = Table[With[{i = i}, #*i*i &], {i, 0, 4}]
functionList[[3]][10]
{#1 0 0 &, #1 1 1 &, #1 2 2 &, #1 3 3 &, #1 4 4 &}
40
The With is used to get the values inside Function, which has the HoldAll attribute, as described in: Using pure functions in Table
4
First, I'd like to point out that your "JoinH" function is already implemented by Join:
a = {{1, 2}, {3, 4}};
Join[a, a + 5, 2]
{{1, 2, 6, 7}, {3, 4, 8, 9}}
Second, you don't need Flat or whatever if you write the function to natively handle multiple arguments:
jh[m__] := Transpose[Join @@ Transpose /@ {m}]
jh[a, jh[a + 5, a + 11]]
jh[a, a + 5, a + ...
8
From documentation:
Nearest[{elem₁ -> v₁, elem₂ -> v₂, …}, x] gives the vᵢ corresponding to the elemᵢ to which x is nearest.
Nearest[data] generates a NearestFunction[…] that can be applied repeatedly to different x."
We can use these two points as below.
pts = RandomReal[{-1, 1}, {1000, 3}];
nf = Nearest[Thread[pts -> ...
10
A little bit more. Still not fully diagnosed, but the problem isn't due to DSolve
... :
s1 = DSolve[{x'[t] == f*x[t] (1 - (x[t]/b)) - l x[t]}, x[t], t];
s2 = DSolve[{x'[t] == e*x[t] (1 - (x[t]/b)) - l x[t]}, x[t], t];
And the problem shows up when matching the initial condition:
Solve[(x[t] /. s2[[1]] /. t -> 0) == 4/10, C[1]]
(*
{{C[1] -> ...
6
The problem can be reduced to the DSolve expressions:
DSolve[{x'[t] == a*x[t]*(1 - (x[t]/b)) - l*x[t], x[0] == 0.4}, x[t], t]
DSolve[{x'[t] == h*x[t]*(1 - (x[t]/b)) - l*x[t], x[0] == 0.4}, x[t], t]
One can see that alphabetical order appears important:
With[{a = Symbol@#},
Shallow @ DSolve[{x'[t] == a*x[t]*(1 - (x[t]/b)) - l*x[t], x[0] == 0.4}, x[t], ...
4
After you call Get, you run a packet loop, waiting for the ReturnPacket. But you never read or discard the contents of that ReturnPacket. That means that the whatever expression that Get returns (it will be the result of last evaluation in the myscript.m file, perhaps the symbol Null) is still waiting on the link. Then you call your function and wait for the ...
3
Forgive me my ignorance to not try your sample code and instead give you an idea for a completely different approach. Since you are just launching a MathKernel in your MathLink program, why don't you use the -run option, to load your package during the launch?
You can test this directly in the front end
kernel = LinkLaunch[
First[$CommandLine] <>
...
3
You will need to make pnorm into a black-box function so that it never tries to evaluate symbolically. Also it is probably worthwhile to use NMinimize/NMaximize in both. Finally the minimax process seems to behave better if you take pth powers in the inner optimization.
pnorm[aa_, p_] /; MatrixQ[aa, Element[N[#], Reals] &] := Module[
{m, n, x, y, f, ...
3
I'm not sure what you're looking for in a 'right' answer, but here are some ways to take (and view) the derivative of your function:
f'[x]
D[f[x], x]
Dt[f[x], x] // TraditionalForm
Dt[f[x], x] // FullSimplify // TraditionalForm
Check the documentation for D and Dt and pay close attention to syntax (ie. where all the punctuation and arguments go)
12
You need partitioning, Partition and parameters: 2 for pairs, 1 for unit overhang/offset, and then averaging each pair, using Map, short-notated /@.
Partition[{a, b, c, d}, 2, 1]
{{a, b}, {b, c}, {c, d}}
These will all make the averages:
Mean /@ Partition[N@badSource, 2, 1]
MovingAverage[N@badSource, 2]
ListConvolve[{{.5}, {.5}}, badSource]
...
3
Section 2.3.1 of the document you linked details how you can send things to the kernel.
The main point here is that you can send a function definition like you send any other Mathematica input. I doesn't matter that it's a function definition. It's just an expression like any other. If you send as an expression, remember its full form: ...
2
Maybe this
LongestCommonSubsequencePositions["DOLORE","LOREM"]
LongestCommonSubsequencePositions[{"D","O","L","O","R","E"},{"L","O","R","E","M"}]
{{3,6},{1,4}}
{{3,6},{1,4}}
LongestCommonSubsequencePositions["DOLORE", "LOREMIJKLMKJLKJLKJLJK"]
LongestCommonSubsequencePositions[{"D", "O", "L", "O", "R", "E"}, Characters@"LOREMIJKLMKJLKJLKJLJK"]
{{1,4},{3,6}}
...
3
Let's read what the docs say:
Root[{$f_1, f_2 ,\ldots$}, {$k_1, k_2 ,\ldots$}] represents the last coordinate of the exact vector {$a_1, a_2, \ldots$} such that $a_i$ is the $k_i$th root of the polynomial equation $f_i(a_1, \ldots, a_{i-1}, x)=0$.
This, though a bit confusing, is a pretty accurate and straightforward description. Let's assume two ...
2
First consider :
Root[327680000000000000 - 1280000000 #1^8 + #1^16 &, 7] // ToRadicals // FullSimplify
(-2 - 2 I) 5^(3/4) (10 - 2 Sqrt[5])^(1/8)
now substitute #1 with this number in #1 + 25 #2 + 25 #2^5 & switching #2 to #1 and compare with your original Root object :
Root[(-2 - 2 I) 5^(3/4) (10 - 2 Sqrt[5])^(1/8) + 25 #1 + 25 #1^5 &, ...
9
You need to specify assumptions:
In[1]:= FunctionExpand[StirlingS2[n, 10], n > 0 && Mod[n, 1] == 0]
Out[1]= -(1/362880) + 2^(-8 + n)/315 + 1/135 2^(-7 + 2 n) +
1/315 2^(-8 + 3 n) - 3^(-3 + n)/1120 +
1/5 2^(-7 + n) 3^(-3 + n) - 5^(-2 + n)/576 +
1/567 2^(-8 + n) 5^(-2 + n) - 7^(-1 + n)/4320 - 9^(-2 + n)/4480
3
There are many ways to proceed. For example one can try FindLinearRecurrence giving the linear recurrence generating a given sequence of functions:
FindLinearRecurrence[{a + 1, a^2 + 2 a + 1, a^3 + 3 a^2 + 3 a + 1}] // Simplify
{1 + a}
now it might be the best way to use e.g. RSolve:
RSolve[{f[n + 1] == (a + 1) f[n], f[1] == a + 1}, f[n], n]
...
12
I know two approaches to this:
In[1]:= FullSimplify[SeriesCoefficient[ArcTan[y], {y, x, n}] n!, Element[n, Integers] && n > 0]
Out[1]= 1/2 I ((-I - x)^n - (I - x)^n) (1 + x^2)^-n Gamma[n]
and
In[2]:= FullSimplify[InverseFourierTransform[(-I k)^n FourierTransform[
ArcTan[x], x, k] , k, x], Element[n, Integers] && n > 0]
...
3
I'm not sure why it doesn't work for you. Try using the full path to the kernel executable. The following works for me on OS X:
./factor -LinkMode Launch -LinkName '/Applications/Mathematica\ 9.app/Contents/MacOS/MathKernel'
I do have a symlink to MathKernel in a location that's in the PATH, but if I use that, as in ./factor -LinkMode Launch -LinkName ...
4
As in my comment above, here's a way that works even if a and/or i have global values:
getA[f_, total_] := Module[{result},
result = Solve[Integrate[f[a, i], {i, 0, 255}] == total, a];
If[Length[result] == 0, Return[False]];
a /. First@result // N
];
plotShow[f_, max_] := Block[{a, i},
a = getA[f, max];
If[! a, Return[]];
Print[f[a, ...
0
is it too early to add an answer to my own question?
I just found a way to do it using With:
tform1[mult_, set_, pos_] :=
With[{posf = pos[set]}, (#*mult) & /@ set[[posf]]];
t1set = tform1[10, set, posE]
This works very similar to @bill s' answer.
5
You have defined a function posE[set] but when you call it, it has no argument. You must call it with an argument. So change the function definition to
tform1[mult_,set_] := (#*mult) & /@ set[[posE[set]]];
If what you are really after is a list that goes from 20 to 500 by 20s (which is what this code seems to do) then there is much better way to do ...
1
Plot[IntImpTri[0, 4, r], {r, -1, 5}, Exclusions -> None]
The problem is usually caused by discontinuities in the derivatives
14
Have a look at this;
http://reference.wolfram.com/mathematica/guide/MathLinkCLanguageFunctions.html
I haven't used it in C/C++ but it works fine in C# and Java. Basically you create a connection to a Mathematica kernel and then pass it native data types. Works nicely.
Here is some sample code in Java that I used when I first did this;
import ...
5
I can't tell what you're doing either, but here's an idea using Nearest:
l1 = Table[{x, 1 + x^2}, {x, -2, 2, .005}];
l2 = Table[{x, 1/2 x^2}, {x, -2, 2, .005}];
d = {#[[1]], EuclideanDistance[#, First@Nearest[l2, #]]} & /@ l1;
ListLinePlot[{l1, l2, d}]
I don't know if there's a formal name for this nearest-point distance. In any case, the ...
3
Witness the following:
With[{n = 7},
Function[Evaluate[ArrayPad[Slot /@ Range[n], {0, n - 1}, "Reflected"]]]]
{#1, #2, #3, #4, #5, #6, #7, #6, #5, #4, #3, #2, #1} &
With[{n = 7},
Function[Evaluate[ArrayPad[Slot /@ Range[n], {0, n}, "Reversed"]]]]
{#1, #2, #3, #4, #5, #6, #7, #7, #6, #5, #4, #3, #2, #1} &
4
How about this:
g[n_]:=Function @@ {Slot /@ Join[#, Reverse[#]]&@Range[n]}
For a random order:
gr[n_] := Function @@ {Slot/@Array[RandomInteger[{1, n}] &, 2 n]}
All in all you can replace Range[n] with your own order of integers depending what you want to accomplice.
3
I'm not really sure if what you are doing makes any sense, but this code seems to implement that dubious thing:
f1[i_] := a43 (-Sqrt[(16 tot5*i)/3 (1 + tot5/(27 i^3))] - (2 tot5)/(9 j*i)) + a43;
f2[i_] := a32 (Sqrt[(16 tot4*i)/3 (1 + tot4/(27 i^3))] - (2 tot4)/(9 j*i)) + a32;
s[i_] := (EuclideanDistance[f1[i], f2[i]]/(3/2 - 4/3))^2;
n = ...
1
Table[Simplify[f[Pi n], Element[n, Integers]], {f, {Cos, Sin}}]
(* {(-1)^n, 0} *)
Simplify[Cos[Pi n/2], Element[n, Integers]]
(* Cos[(n Pi)/2] *)
You could do something about this differently I guess ... what, as @J. M. suggests, you could append an assumption that n is odd eg.
Simplify[Cos[Pi n/2],
Element[n, Integers],
Assumptions -> Mod[n, 2] == ...
1
TimeConstrained[expr, t, failexpr] returns failexpr if the time constraint t is not met.
1
You can also use Boole ... provided you are happy for your func to return 0 when the domain conditions are not met. For example:
f[x_, y_] := Boole[x > 0 && y > 0] Cos[x] Sin[y]
Plot3D[f[x, y], {x, -1, 2}, {y, -1, 2}]
Alternatively, you can set up a Piecewise function with explicit settings for what is to be returned (e.g. Indeterminate) ...
2
For completeness, I'll just add the textbook definition of the $n$-th root of a complex variable:
root[x_, n_, branch_: 1] :=
Simplify[Power[Abs[x], 1/n] Exp[I (Arg[x]/n + 2 Pi (branch - 1)/n)]]
root[1, 2, 2]
(* ==> -1 *)
Here, x is an arbitrary complex number, n is the power of the the equation $z^n = x$ we're trying to solve, and branch is the ...
2
While Mathematica follows the conventional philosophy about inverses of functions that are not one-to-one, it does provide tools for dealing with other approaches. If Solve is too cumbersome, one can write one's own versions:
root[n_Integer, expr_] := expr^(1/n) ((-1)^(2/n))^Range[0, n - 1];
root[n_Integer, Power[expr_, m_Integer]] /; Divisible[m, n] :=
...
1
The observations I made I got from messing around with Unevaluated, Trace, Hold and FullForm.
My intuition was that we could make get the same behavior of Sqrt for numbers as for Symbols using Unevaluated. However, there is a a rule for Sqrt[anything] that must look like this
HoldPattern[Sqrt[anything_]]:> Power[anything, Rational[1,2]]
So an ...
0
Whenever I want to do cheap adaptive sampling along a function, I used to do what David did back in old versions of Mathematica. Nowadays, I proceed like so:
pts = Cases[Normal[Plot[Sin[x], {x, 0, 2 π}, Mesh -> All]], Point[pt_] :> pt, ∞];
ListPlot[pts] should yield an image similar to the one in David's answer. A similar procedure can be done for ...
3
Just for fun, here's a recursive approach:
accSum[{}] := {}; accSum[{x_}] := {x};
accSum[{r___, x1_}] := Join[{Total[{r, x1}]}, accSum[{r}]];
Usage:
list = {11.5575, 11.397, 5.52734, 4.0878, 2.54815, 1.86652, 2.55028,
2.14952, 1.6242, 1.34117}
accSum[list] // Reverse
Gives:
{11.5575, 22.9545, 28.4818, 32.5696, 35.1178, 36.9843, 39.5346, 41.6841, ...
10
Accumulate is absolutely the most idiomatic and appropriate answer here. However since Mathematica is very powerful at list manipulation, it might be illustrative to show you couple of other ways of doing the same thing, just so you learn to think outside of mainstream procedural ways.
1. Using FoldList:
This is a functional way of doing exactly what you ...
8
Easy Solution
v = Accumulate[list]
Gives Output
{11.5575, 22.9545, 28.4819, 32.5697, 35.1178, 36.9843, 39.5346, \
41.6841, 43.3083, 44.6495}
2
You can define the derivative of a function easily: D[f[t], {t, 1}] is the first derivative of the (unspecified) function f[t]. So your expression is:
{D[f[t], {t, 2}], D[g[t], {t, 2}]}.{-D[g[t], {t, 1}], D[f[t], {t, 1}]}
which nicely returns the desired expression.
You will likely benefit from exploring the documentation a little. For instance, had you ...
4
For the two curves use the command:
g1 = ParametricPlot3D[{{t, 0, t^2}, {0, u, u^2}}, {t, -10,10}, {u, -10, 10},
BoundaryStyle -> Thick];
in addition, if you want also to have the two $x-y$ and $y-z$ planes in the plot:
planes = ContourPlot3D[{x == 0, y == 0}, {x, -10, 10}, {y, -10, 10}, {z,0, 100},
Mesh -> False, ContourStyle -> ...
Top 50 recent answers are included







