For questions on writing functions (pure or using Set/SetDelayed) for any purpose, including the features that may be incorporated in those functions, such as options, patterns and conditions.

learn more… | top users | synonyms

6
votes
1answer
97 views

How to track Part of Symbol or How to symbolize Parts of Symbol without copying data

Let's create simple function: SetAttributes[h, HoldFirst]; h[data_] := Dynamic[Grid[data], TrackedSymbols :> {data}] test = RandomReal[1, {2, 2}]; h[test] ...
8
votes
1answer
105 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
1
vote
2answers
65 views

Expressions containing globally undefined symbols inside a function where they are defined

Consider the following code. f[a_,b_]:=x; Clear[a,b]; x=a+b; f[1,2] It returns a+b. However, from a certain viewpoint, one ...
1
vote
0answers
52 views

The problem (time) caused by ?NumericQ

In here, I asked a related (similar) question. Using ?NumericQ seems have slowed down the entire calculation. But I now have a more serious problem: ...
0
votes
1answer
552 views

Computing the fundamental matrix and its monodromy matrix

Assume $\dot x(t)=v(x,t)$ is a $T$-periodic, with respect to $t$ dynamical system. That is: $x\in\mathbb R^n,\quad t\in\mathbb R,\qquad v(x,t+T)=v(x,t)$. Let $x_0$ be a smooth periodic solution in ...
1
vote
0answers
61 views
0
votes
0answers
58 views

How to define a two-variable sequence?

I would like to define the following function but I am having a problem. I would like Mathematica to "understand" what I mean by writing "+...+". For example, for large values for $n$ and $m$ it takes ...
2
votes
1answer
52 views

How to protect function variables [duplicate]

I am sure this has been asked before, but I can't seem to find the right solution. I want a function to be the solution to a differential equation ...
3
votes
1answer
134 views

Function that returns the second argument of Reap

I'm trying to create a function that returns only the second argument of Reap. Here's my naive guess of how it would go : Reap2[x_]:= Flatten[Reap[x][[2]],1] ...
1
vote
1answer
79 views

Piecewise smooth rectangle pulse

I am wondering how to create a single rectangle pulse with nonlinear riding and falling edges on both sides. I don't know whether it is better to do it piecewise or to mathematically transform the ...
2
votes
1answer
31 views

How shortening argument test when declaring functions?

When defining some functions which depend in many arguments sometimes we need to include question Q constraints to diminish processing time. My question is simple, there is a way to shorten a long ...
4
votes
2answers
162 views

A better way of handling Module output?

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of ...
0
votes
0answers
71 views

How to conveniently stop a programme without aborting the computation

I have a programme that will have to run for a few days. However, I would like the user to have the option of stopping the computation of the code, without aborting it, when he desires, without ...
1
vote
1answer
100 views

Quantize a real-valued function?

I'm wondering if there's a built-in way in Mathematica to take a function whose output values are continuous, and quantize it to produce a step function. For example, I have the function ...
0
votes
1answer
49 views

Defining a function with algebraic parameters

I am trying to define and use a function to represent the eigenstates of a Quantum Harmonic Oscillator, which have the general form: $$\psi_{n}(x) = \frac{1}{\sqrt{2^{n} n!}}\left(\frac{m \omega}{2 ...
1
vote
1answer
91 views

Why doesn't Thread work in this case?

The following function is supposed to pick the element of the list with highest weight, i.e., with Thread [f[{{a,1},{b,2}}]] it should make the variables ...
6
votes
3answers
178 views

How to define a nested function like this?

For example, I want to calculate the limit of $\frac{1}{1+\frac{2}{1+\frac{3}{1+\ldots}}}$. So I want to define: $f(1)=1$ $f(2)=\frac{1}{1+2}$ $f(3)=\frac{1}{1+\frac{2}{1+3}}$ Then I calculate ...
3
votes
1answer
91 views

Define and plot a piecewise function with parameters [closed]

I have this function , but i do not know how to define it in mathematica \begin{equation} f_t(x)=% \begin{cases} i& \text{$\frac{1}{1+i}<|x-t|\le \frac{1}{i}$} \\ ...
1
vote
1answer
59 views

Changing default behavior of Series to give slightly different SeriesData

I am working with complicated expressions expr that contains the symbolic function f[x] which I know to have a pole at ...
2
votes
3answers
78 views

How to test a condition for a list and output all the expression that is true?

Suppose that I have a list lis={x-y,x+y,x*y}; and a function f[a_,b_]:=#/.{x->a,y->b}& I want to output all the ...
2
votes
1answer
238 views

How to make a function from body with variable number of arguments?

Suppose I have an UNKNOWN function, which generates formulas with given number of x[i]. ...
1
vote
1answer
43 views

Composition of Multivariable function

I am trying to compose functions of xleft, maybe like xleft[xleft[f]] or even higher order. And here is my function: ...
0
votes
0answers
45 views

Question about treillis structures

I have difficulties finding a command to plot multiple segments connected in nodes in Mathematica. Ultimately, my goal is to modelise something like this by entering nodes coordinates. Also, the ...
4
votes
2answers
63 views

Count the number of element repeats at various distances

I have a function of x: f[x_] := Abs[Extract[list, x + n] - Extract[list, x]] I then apply this: ...
1
vote
1answer
181 views

How can I call the R-programming packages in my Mathematica?

I am trying to call the R-programming package called highfrequency in my Mathematica so that I can use its function rCov. I ...
2
votes
2answers
113 views

Understanding `NestList` with multiple functions

General question For a function f[n, a], where a is a list and n is a whole number, what is ...
2
votes
2answers
99 views

Can't use my own discontinuous coefficient within NDSolve

I want to evaluate the pendulum's motion with changing equilibrium state given by eqilValue[t]: ...
6
votes
2answers
154 views

Using a held option

Woud there be an elegant way to use a held option ? For example if I need to pass by reference several variables but I don't always need all of them, it can be useful. For example if I want to append ...
0
votes
1answer
74 views

Recursive functions taking list arguments

I wish to create a function that takes lists (e.g. {plus,plus,minus,0,minus,plus}) and uses the input list to calculate a corresponding value using a set of ...
1
vote
1answer
70 views

Help creating a simple function

Can someone help me to create a function. The truth I'm new in Mathematica and I don't understand well the way functions work in I. I need a function that animate some rectangles according to a curve ...
2
votes
1answer
32 views

Linear function on strings using UpValues

I have two functions Sup and Sdown that take a string of letters with allowed characters ...
0
votes
1answer
96 views

Optimizing Chi-square distance between two distribution

I want to program the chi-square between two distribution Xu and Vu with length N. This function is defined as below: Xu and Vu are real and positive. ...
1
vote
2answers
50 views
4
votes
2answers
88 views

Function pattern to support listed and non-listed arguments [duplicate]

I often want to write functions that take as an argument either a) a rule or b) a list of rules. As an example, when using Replace with just one rule, it does not ...
3
votes
1answer
58 views

Defining a function that prints both the name and value of a variable defined locally within the calling function

To save time when debugging with Print statements, I'd like to define a function -- call it PrintVal -- that prints both the name and value of a variable defined locally within the calling function. ...
0
votes
1answer
42 views

Error: in the part assignment is not a symbol [duplicate]

I am trying to set part of a Doubly-indexed list aInitialPopulation to cNumHeg. Specifically I am trying to set the 3rd part of the list aInitialPopulatiom[[cNodeNumber]] to cNumHeg. My (attempted) ...
2
votes
1answer
60 views

What are the applications of using functions whose heads are also functions?

I noticed that it is possible to construct something like f[x][y]. Displaying this in TreeForm indicates that this expression ...
5
votes
2answers
569 views

Variable that changes itself

I am trying to store a boolean value into a variable with the additional property that the value changes each time the variable is called. ...
1
vote
1answer
392 views

Fitting parameters of user defined function returning discrete values

So, I'm fairly new to Mathematica (apologies in advance for the noobiness), and I'm having some problems regarding fitting the parameters of a user defined function to data. I'll give a representative ...
4
votes
2answers
67 views

How to sum values in the list that belongs to same week

I need to sum values that belongs to same week. For example, I have the list x with one column and n rows. Format: ...
0
votes
2answers
40 views

How do I localize an assignment to expression (instead of symbol) within scoping construct?

Is it possible to localize an assignment to an expression (defined via UpSet) instead of a Symbol within a scoping construct? ...
2
votes
1answer
50 views

Searching for symbol except within subexpressions with a particular Head

I would like to write a searcher like FreeQ that looks for a Symbol (or Head) in an ...
6
votes
1answer
180 views

Parentheses for negative integers

Trying to standard create prime factors presentation, but can't seem to remove the parentheses: ...
3
votes
1answer
42 views

Mapping also for one element (or telling it how to do so if head got changed)

Some code generates a polynomial, and has the form: expr = Plus[a[i],b[i],c[i],d[i]] I need to apply f to each term. To do ...
1
vote
0answers
24 views

How to make Expand work right in a function definition?

This works just fine: Derivative[n_, 1][g][x_, v_] := D[g[x, v], {x, n + 2}]/2 + D[D[g[x, v], x]^2/2, {x, n}]; h = Expand[D[g[x, v], {v, 3}]] I would like to ...
1
vote
1answer
77 views

How to form a variable from matrix elements

I have matrix with n rows and 2 columns, say m = {{x11, y12}, {x21, y22}, ..., {xn1, yn2}. Now, I want to form three new variables for example: ...
8
votes
3answers
119 views

Defining a custom Plus for a specific class of arguments

I'm trying to develop an domain-specific language with Mathematica. I want to define a customized behavior for the Plus function when applied to my data. For ...
1
vote
1answer
70 views

Plotting a function in {X(Y),Y} space

The code below generates some data and defines a function f[x,cb]:=NMaximize[expr,x] that upon being evaluated returns: {function value at max $x$,the function maximizing $x$}. ...
6
votes
3answers
175 views

How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the ...