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

2
votes
4answers
2k views

How to write a differential operator in Mathematica

I have a very basic question. I am trying to code the LHS of the differential equation: ...
3
votes
1answer
259 views

NestList with a list inside?

I'm new to Mathematica, and was looking for more efficient ways to implement the Do loop below. a, ...
2
votes
1answer
250 views

Make a scatter plot

Considering I have weather data for last 10 years for London, how can I make a scatter plot that will show the previous day's temperature as a function from today's temperature?
26
votes
1answer
444 views

which is better, using Assert[] or manual checks on arguments and other computations?

I never used Asserts in Mathematica, but trying to see what advantage they have over just argument check and additional definitions of the function to capture unwanted input. For example, which one ...
20
votes
3answers
390 views

Why x = x doesn't cause an infinite loop, but f[x_] := f[x] does?

If I execute: In[1] := x = x Out[1] = x and then I evaluate the symbol x: ...
25
votes
1answer
518 views

How to augment the realm of functions Mathematica thinks it knows how to integrate symbolically

My question involves extending the functionality of Integrate over specific integrals in the most generic manner. Specifically, is it possible to "hack into" ...
14
votes
5answers
876 views

Picking random items out of a list only once

I'm trying to create a function that randomly returns a value from a list but remembers the values that have been given before. At the end when the list is empty it should return an empty list. ...
15
votes
1answer
374 views

How to represent a continuous monotonic phase of Airy functions?

Note: In this question I am concerned only with real-valued variables and functions. DLMF, ยง9.8 Airy Functions, Modulus and Phase, formula $9.8.4$ defines the phase of Airy functions: ...
11
votes
3answers
401 views

Pass by reference for an option argument

Pass by reference can be faked by using HoldAll or something similar in the definition of a "Function". But can pass by reference be faked for symbols passed as ...
8
votes
2answers
168 views

How to correct this, `f[a_] := FindRoot[eq == 0, {x, 0.5}]`, where `eq` contains a parameter $a$, without using `eq[a]`?

I want to solve an equation with a parameter $a$, and define the result as a function of $a$. It is something like this: ...
8
votes
3answers
208 views
7
votes
3answers
278 views

How to set a TraditionalForm output for a symbol

How do I set a TraditionalForm output for a particular symbol/function? In particular I would like my user-defined symbol ...
6
votes
3answers
112 views

Explicit use of Evaluate not behaving the way I expect it to

I discovered a strange behavior of the Evaluate function. Case 1: ...
6
votes
2answers
213 views

Composition of functions using slots

Apolgies if this has been asked and answered, but I'm not even sure what to search for. I have two functions defined as follows: ...
5
votes
3answers
1k views

Make a differentiable smooth sawtooth waveform

So, I am trying to understand the effect of sawtooth waveform on a mechanical system model. For example, a system of a plate and a box on it. And then we try to oscillate the plate. In order to do ...
3
votes
1answer
131 views

How can I speed up my compiled function that is returned by another function?

Overview I have a function that acts like the Interpolation on sparse $n$-dimensional data using a simple implementation of RBF interpolation method. I want my ...
12
votes
1answer
286 views

Modules that initialize themselves on first call

I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
9
votes
2answers
315 views

Define Log so that negative reals evaluate on lower edge of branch

I need to get Mathematica to evaluate the logarithm of a negative real number using the lower branch instead of the upper branch, so that while ...
5
votes
2answers
881 views

How to find the non-differentiable point(s) of a given continuous function?

For example, the non-differentiable point of the function $f(x)=|x|$ is at $x=0$. How to find the non-differentiable points of a continuous function that is defined numerically?
4
votes
3answers
179 views

Constructing a function with Flat and OneIdentity attribute with the property that otimes[a]:>a

I want to have a function (called otimes) with the following two properties: it has the attribute OneIdentity and ...
0
votes
2answers
118 views

How to assign special colors to the output of DensityPlot?

I have created the following function for plotting ...
0
votes
1answer
139 views

iterative programming

i have 3 equations with 3 variables and two constants(to be substituted repeatedly). the value of the variables we got by applying nsolve on the system of 3 equations assuming some initial value for ...
0
votes
1answer
304 views

Function to marginalize a table over given parameters

Let us say I have generated a table tab with entries {p1,..,pd,chi2[p1,..,pd]} of dimension ...
9
votes
1answer
229 views

How to define function that can change patterns

Given time series data as follows: data = {{{2011, 1, 13}, 10}, {{2011, 2, 14}, 17}, {{2011, 3, 15}, 20}, {{2012, 1, 16}, 30}, {{2012, 2, 17}, 40}}; ...
3
votes
9answers
2k views

How to find palindromic numbers (Project Euler #4)?

I'm trying to solve the fourth Project Euler's problem with this: ...
2
votes
1answer
192 views

Finding average error of the data

Considering I have some data, and I want to fit the data to a linear fit and find the average error from the linear fit, how can I do that using Mathematica? I have ...
2
votes
3answers
359 views

Constraining function found by NDSolve to stay positive

I am trying to constrain my variables in NDSolve so that they stay positive for the integration interval. Is is possible to do that? Using Assumptions as in ...
2
votes
3answers
477 views

Generating pairs of additive and multiplicative factors for integers

Given an integer $n$, I want to get two lists: a) the set of pairs of the divsors $a,b$ into exactly two factors $n=a\cdot b$, b) the set of pairs $a,b$ of two summands $n=a+b$. The code I ...
2
votes
0answers
331 views

Generating a function which outputs possible chemical reactions

I want to make a list of chemical reactions and I write them down in a $\require{mhchem}\LaTeX$ format. They are of the following form $$NA_n^i+MB_m^j \rightarrow \hat NA_{\hat n}^{\hat i}+\hat ...
1
vote
1answer
89 views

Passing function as argument

I've been mucking around with passing functions as arguments to other functions and via trial and error seem to have something that works. I was wondering if someone could have a quick look at some ...
0
votes
1answer
114 views

A problem with set values

The following code is just an example to clearly explain my problem. Assume that : ...
13
votes
1answer
344 views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
6
votes
1answer
105 views

A question about two ways to use Default

Usually we define a function with default values by the syntax x_:default, but there is another technique to do this by registering a global default value with ...
11
votes
4answers
497 views

Assessing argument type in set delayed function definitions

I'm wondering how to properly assess the type of arguments passed to functions defined with :=. I want my functions to be the most efficient, unambiguous and clear ...
11
votes
7answers
2k views

Recursive function with if-statement

I am trying to represent the following function definition in Mathematica: $$\begin{align*} f(1)&=1 \\ f(2n)&= \begin{cases}f(n) & \text{if}\space n\equiv0\pmod{2} \\ 2f(n) & ...
10
votes
3answers
830 views

Passing down arguments

In R, one can use ... to pass arguments down to another function. For example ...
8
votes
1answer
296 views

How can I define a Listable function only apply to a vector?

Consider the function f which should behave as f[{{1, 2}, {3, 4}, {5, 6, 7}}] ...
8
votes
3answers
369 views

Where in the documentation can I find a list of function argument types?

Recently, I learned that it is possible to assign "types" to function arguments in the definition of a function. Suppose I have a function stringFun that does some ...
13
votes
0answers
165 views

func::usage needs to be run twice? [duplicate]

There is an annoying problem that I can't figure out why it's happening. I have defined a usage for a my function tensorA: ...
11
votes
2answers
666 views

How to plot a barycentric line

I want to plot a barycentric function on an equilateral triangle (ternary plot). For example f1 = {Abs[Sin[x]], Mod[x, 2], Abs[Cos[x]]}; At the moment I evaluate ...
7
votes
1answer
829 views

How to find the smallest root

I have a continuous, differentiable, monotonic, bounded function called F[t]. If t -> Infinity then ...
7
votes
4answers
1k views

Force function to make assumptions about its input variables in Mathematica

I'm trying to force a Mathematica function to make an assumption about its input variables. In my case, I'm trying to define a function to return the pdf of a Gaussian. But let's use a simple toy ...
6
votes
3answers
176 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 ...
6
votes
2answers
153 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 ...
6
votes
1answer
137 views

Creating a Block from a list of rules

How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments? The function needs to be ...
5
votes
2answers
145 views

How to understand the process of ListCorrelate when it in two-dimensional condition?

Toady,I learn a function ListCorrelate,and I can understand process of execution his result in one-dimensional. However,the ...
3
votes
1answer
117 views

Custom operators; custom pattern matching with pure functions

What I'm trying to achieve in Mathematica is the creation of a binary operator whose operands are both pure functions over the natural numbers. The result of the operator should be another pure ...
14
votes
3answers
242 views

What's wrong with this pattern of the form Except[Repeated[…]]?

I'm trying to build a head that recognizes molecules. Here's the code ...
10
votes
2answers
370 views

How do I get Mathematica to return a function call unevaluated?

How do I get Mathematica to return a function call (conditionally) unevaluated? I expect this may use the slightly-mysterious Hold function. As a toy example, ...
7
votes
1answer
310 views

How can I define a function that can accumulate its parameter?

For example, write a function add: add[3] return 3 add[4] return 7 add[10] return 17