Questions tagged [function-construction]

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.

Filter by
Sorted by
Tagged with
48 votes
6 answers
6k views

Can I make a default for an optional argument the value of another argument?

I'd like to define a function with several optional arguments, some of which default to the value supplied for other arguments. For example, I'd like to be able to write something like ...
orome's user avatar
  • 12.8k
2 votes
1 answer
302 views

Difference between function definitions [duplicate]

Possible Duplicate: Functions vs. patterns I thought about describing the context in which I faced this problem, but I figured it is general enough and clear enough to pose it as it is. What ...
yohbs's user avatar
  • 7,046
19 votes
2 answers
20k views

How to define a differential operator?

I have a linear differential operator, for instance, $L\left (\partial _{t} \right )=\partial _{tt} - 3\partial _{t} + 2$. I use it in 2 different ways: apply the operator to a function: $L\left (\...
prazuber's user avatar
  • 429
-2 votes
2 answers
2k views

How to correctly call a Module within another Module?

How to embed and call the correct embedded modules inside another Module? Say: ...
sebastian c.'s user avatar
  • 1,963
13 votes
3 answers
5k views

How do I designate arguments in a nested map?

Say I have two lists, list1 = {a, b, c} list2 = {x, y, z} and I want to map a function f over them to produce ...
rayhem's user avatar
  • 355
0 votes
1 answer
1k views

Multivariate Function Approximation With a Large Dataset

I have a nice amount of data from a trading strategy I am working on, where I have two different liquidity parameters as x, y variables. Before entering a trade I am taking the moving average of <...
Brandon's user avatar
  • 11
21 votes
2 answers
670 views

Pattern that matches colors

Suppose I'm writing a function that takes a color as a parameter; for example: drawShape[color_] := Graphics[{Style[Disk[], color]}]; But if the caller inputs an ...
Mechanical snail's user avatar
7 votes
2 answers
279 views

How is OptionsPattern[] defined? Can I replicate this?

I am writing several functions which all take similar input parameters (a complicated nested list structure). Is there some way for me to define a pattern that can be used in multiple places and ...
jtbandes's user avatar
  • 1,422
1 vote
1 answer
285 views

Find the derivatives of arbitrary order of a function found using NDSolve

For the following initial value problem BC[b_]:=x[0]==b; eq[b_]:=NDSolve[{equation==0, BC[b]}, x, {t,0,1000}]; body[t_,b_]:= x[t]/.eq[b]; one could define the ...
auxsvr's user avatar
  • 507
1 vote
1 answer
99 views

Why function is evaluated backward?

I have a simple function: f[x0_,y0_]:= Module[{x=x0,y=y0},For[i=0,i<2,i++;Print[x],Print[x+y]];] So when I call it: f[2,3]...
sebastian c.'s user avatar
  • 1,963
4 votes
3 answers
863 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 ...
Nikolaj-K's user avatar
  • 1,485
2 votes
0 answers
675 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 MA_{\...
Nikolaj-K's user avatar
  • 1,485
1 vote
0 answers
494 views

Return Functions from parallel execution of module

I wrote a module which numerically solves an ODE using NDSolve. The result from this is e.g. either ...
mincos's user avatar
  • 453
1 vote
1 answer
141 views

Defining functions from expressions of variable lengths

I have a list of symbols that I have defined using d = Table[Symbol["d" <> ToString[i]], {i, 1, n}] giving ...
user4667's user avatar
7 votes
2 answers
5k views

Building matrices by attaching vectors (columns) and matrices

Having the need to attach a column to a matrix or to join matrices to make longer rows is an operation that I use very frequently and I find the Join function ideal ...
VLC's user avatar
  • 9,798
4 votes
1 answer
2k views

Converting expressions into functions

I have defined an expression, such as a = x I would like to convert this into a function, such as f[x_]:=x However, in my ...
Carl Morris's user avatar
1 vote
0 answers
136 views

Non Commutative Multiply [duplicate]

Possible Duplicate: Non-commutative symbolic linear algebra I want to multiply two matrices, for example, A = {{e, f}, {g, h}} B = {{a, b}, {c, d}} Using <...
rodrigo's user avatar
  • 11
1 vote
1 answer
2k views

Matrix Multiplication Modulo 2

I would like to perform matrix multiplication modulo 2. Hence, instead of the usual: A.B I did: ...
Pavithran Iyer's user avatar
0 votes
1 answer
626 views

How to generalize a formula?

This is a piece of my code: ...
Anixx's user avatar
  • 3,543
0 votes
1 answer
156 views

Bad function evaluation

I am having some trouble with this function ...
pablo's user avatar
  • 723
3 votes
4 answers
277 views

How to create a composite function from a variable number of components?

Background of the problem: A patient takes a drug every $X$ hours with a half-life of $Y$ hours. How much of the drug will be (exactly) in his system after $h$ hours? For $X=24$, and $Y=24$ I made the ...
nilo de roock's user avatar
2 votes
1 answer
158 views

Implementing a For (?) or series or loop (?) for individual results for every x to n

I'm working on improving a modeling formula and have spreadsheets worth of data to use, but my work so far has been quite tedious. I'm essentially pulling 20 rows of data from each separate sheet of a ...
William Everest's user avatar
11 votes
3 answers
6k views

Better solution than returning a list of 3 values?

I have a function (using SetDelayed) that currently returns 3 values in a list. Later on I use the result of this list along with ...
jtbandes's user avatar
  • 1,422
3 votes
1 answer
514 views

Defining a function that handles Dynamic arguments similar to Built-ins

Firstly please note that I'm using simply Dynamic behavior to illustrate my question, I'm not asking how to move a Disk or make ...
jVincent's user avatar
  • 14.8k
3 votes
2 answers
144 views

Why does this function fail when a non-function version of the same code works?

This works: ...
George Wolfe's user avatar
  • 5,422
1 vote
2 answers
201 views

How to mix Unevaluated property of function arguments with expression assignments?

This is a follow-up to my (solved) question How to distribute a generic function of two arguments (without evaluating the arguments) Say, I have a binary function ...
janitor048's user avatar
4 votes
3 answers
366 views

How to distribute a generic function of two arguments (without evaluating the arguments)

I guess there is an easy way to do this, but I can't seem to figure it out. What I would like to do is to distribute a generic (i.e. without specifying what this function really does) binary function. ...
janitor048's user avatar
7 votes
4 answers
1k views

Composition of mappings not working as expected

I have two functions $f,g:\mathbb{R}^2 \to \mathbb{R}^2$ and I define a third one $h:\mathbb{R}^2 \to \mathbb{R}^2$ as the composition $$h(x,y) = g(f(x,y))$$ I'm trying to get this function into <...
AvatarOfChronos's user avatar
9 votes
2 answers
896 views

Define product derivative

How do I define the $n$th product derivative of a function in Mathematica? The first product derivative $f^\ast$ of a function $f$ is $$ f^\ast(x)=\exp\left(\frac{f^\prime(x)}{f(x)}\right) $$ The $n$...
Navin's user avatar
  • 235
4 votes
3 answers
1k views

How to pull scalars out of a function that should act on lists?

Suppose I have A = a vecA B = b vecB where a and b are supposed to be arbitrary scalars ...
janitor048's user avatar
2 votes
1 answer
198 views

Specifying form in MemberQ

This seems like a very simple question, the answer to which should be obvious to me. So I won't be offended if this question is closed. But I'm having trouble figuring out the answer. I would ...
Andrew's user avatar
  • 10.4k
4 votes
2 answers
315 views

Function to select a row whose kth element is closest to a given number

I'm trying to write a function that picks out the row of a Matrix whose kth element is closest to some given number that will be given to me. So for example, <...
Amatya's user avatar
  • 6,818
17 votes
1 answer
535 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 ...
George Wolfe's user avatar
  • 5,422
16 votes
2 answers
4k views

NestList on a function of several arguments

I'd like to know if it is possible to use NestList on a regular function of several arguments, such as: ...
Martin Melka's user avatar
8 votes
2 answers
9k views

How to find (numerical) value of a derivative at point?

I have the following function: f[0, 0] = 0 f[x_, y_] := Exp[-(x^2 + y^2)^(-1)] How do I find its partial derivatives at any given point, including $(0,0)$? This ...
mbork's user avatar
  • 949
2 votes
2 answers
225 views

Constraining a random function

This question is a follow-up from here. I have a function that generates a list of correlations between some random variables: ...
Ooku's user avatar
  • 891
9 votes
1 answer
1k views

Functional programming and Do loops

I'm writing a Monte Carlo simulation. I'm using a Do loop to create the different simulations, iterating over different ...
Ooku's user avatar
  • 891
18 votes
1 answer
826 views

Is it possible to Clear all variables (but not functions)?

I have written a Mathematica script in which I define functions and variables. Here is a vastly simplified example: ...
Andrew's user avatar
  • 10.4k
11 votes
2 answers
317 views

can a ColorFunction have side effects?

Can anyone explain this (to me) odd behavior. If I run this code once (fresh kernel) it works as intended. ...
george2079's user avatar
  • 38.8k
3 votes
2 answers
623 views

Using Functions as Arguments to Function

I am trying to make a function like Norm but defined like so: MyNorm[{x_,y_,z_}]=Sqrt[x^2+y^2+z^2]. Mathematica assumes that x, ...
diracdeltafunk's user avatar
10 votes
4 answers
874 views

Generating a position lookup function for an arbitrary list of integers

In my code, I generate a list of integers called numlist. Here is an example numlist: ...
Andrew's user avatar
  • 10.4k
1 vote
2 answers
617 views

Piecewise function with a function as argument

I want to define a piecewise function, which has two arguments, one of which is a function of the other's piecewise argument. I am giving an example ...
Thanos's user avatar
  • 993
1 vote
2 answers
224 views

How can define a set of nested parameters

I would like to know how I to define a function with a nested set of parameters (or whatever you would like to call it). For example, how would I get the following function ...
ShaunH's user avatar
  • 807
2 votes
3 answers
8k views

Variables Inside Functions Not Evaluating

x = 5*y function1[y_] := x function1[5] Am I wrong in thinking that function1[5] should equal 25? Instead this comes out. ...
Cadell Christo's user avatar
1 vote
1 answer
315 views

Calling part of a variable in a user defined function

I have defined a function ...
Jennifer's user avatar
  • 953
3 votes
4 answers
245 views

Pack Solve results into a vector

I am currently using a really easy function to get the eigenvectors of a corresponding eigenspace: ...
Christian Ivicevic's user avatar
11 votes
3 answers
641 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 ...
Andrew's user avatar
  • 10.4k
12 votes
2 answers
488 views

Manipulating OptionsPatterns[]

How do you combine OptionsPatterns? For example, I would like to have a function that can take both options that I specify, and options for all graphs: ...
Andrew Spott's user avatar
  • 1,571
23 votes
3 answers
981 views

Nested definition: How can I define a function with a passed-in expression?

Here's a simplified version of what I'm trying to do: ...
celtschk's user avatar
  • 19.1k
29 votes
6 answers
3k views

Convert an expression to a Function

I need a function which can take an expression and return a pure function based on the symbols in the expression. The symbols might have values so must be protected from evaluation. It is probably ...
Simon Woods's user avatar
  • 84.6k