Tagged Questions

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
1answer
149 views

Rookie mistake in defining a function with Modules?

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of x, y and ...
7
votes
4answers
321 views

Is there a concise form of Which or similar conditional?

Suppose that I want to write a function fun that takes an Integer num as input, and returns ...
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: ...
7
votes
3answers
272 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 ...
2
votes
2answers
588 views

Composing two functions in Mathematica [duplicate]

Possible Duplicate: Composition of mappings not working as expected I am new to Mathematica and I am having trouble defining a certain composition of functions. Let ...
2
votes
1answer
181 views

How to override `?NumericQ`

I have a function myFunc which I is to be displayed cleanly if arguments are symbolic, but which can also be numerically evaluated. I do this by defining a ...
5
votes
3answers
598 views

Define an 'inner product' with AngleBracket

I want to define my own little 'Inner Product' function satisfying properties of linearity and commutativity, and I'd like to use the "$\langle$" and "$\rangle$" symbols to output my results. For ...
3
votes
1answer
228 views

Permuting indices to form fully symmetric tensor with repeated indices

I have written a function (version 8) that takes as an input a list of indices such as {i,j,k} and outputs a fully symmetric tensor function containing $p_i$ and ...
2
votes
0answers
91 views

How can this function's options be used in different places? [duplicate]

I wrote this function to make a ListPlot where the points are colored by a third argument. ...
1
vote
1answer
101 views

How do I define a function that, when given a list of parameters, plots a family of functions?

So far I've got this: draw[a_, b_] := Plot[{f[a, b[[1]], x], f[a, b[[2]], x]}, {x, 0, 5}] The function draw will be used like ...
5
votes
3answers
380 views

Defining functions with special cases

I'm trying to define a function myFunc with a special case: myFunc[x_, y_] = (x^2 - y) Log[x^2 - y]; myFunc[x_, x_^2] = 0; ...
4
votes
2answers
258 views

How to create 'help' `?` for a function (as documentation)

I have defined a function PassVeltB in some complicated way. But now I would like to add a little documentation that gives the user information on how it is to be ...
2
votes
3answers
273 views

How do I construct a pure function to extract data fields from records that are strings?

How can I get from data segment format to the final segment format with a pure function? The data fields are fixed length. There are no separators between the fields. a) list ...
1
vote
1answer
397 views

Combining two pure functions

How to get from the following intermediate result to the final one with just one pure function? ...
9
votes
2answers
310 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 ...
1
vote
2answers
187 views

Function with different set of arguments [duplicate]

Possible Duplicate: How to create functions of arbitrary number of variables? i want to use a Function f[] with different set of arguments. example: ...
4
votes
2answers
183 views

How can I improve this trial division procedure?

My background is procedural programming, so I find this construction quite natural. Is there a way to get rid of the While? ...
4
votes
6answers
219 views

Filter list with different list in it

I have a list like: {{{4, 14}, 1}, {{4, 15}, 1}, {{4, 16}, 1}, {{4, 17}, 1}, {{4, 18}, 1}, {{4, 14}, 3}, {4,15}, {{4, 16}, 2},{4,18}} Now I want to ...
3
votes
1answer
238 views

Evaluating a function on permutations of its arguments

Say I have a function "temp" of $n+1$ variables, $y,z1,z2,z3,...,zn$. I want to test if my function has certain symmetries like swapping $y$ with square of any $z$, swapping any two of the zs, ...
1
vote
2answers
174 views

A function that only evaluates on lists of pairs [duplicate]

Possible Duplicate: Why doesn’t PatternTest work with Composition? I'd like my function to only evaluate when the argument is a list of pairs. It seems like ...
0
votes
2answers
187 views

How to use Solve rules as a function body?

Solve gives me an expression which I want to use as the body of a function. Rather than run Solve every time inside my function ...
12
votes
2answers
231 views

f[arg1, arg2,…,argN] vs. f[{arg1, arg2,…,argN}]

I am trying to reproduce the API of a function (written in R) that accepts an arbitrary number of arguments and handles it the same way as it would handle a single argument that is a list of different ...
2
votes
1answer
181 views

How do I determine if there's an arithmetic sequence within a list? [duplicate]

Possible Duplicate: find subsequences of constant increase Given an arbitrarily long list of integers (let's say they are sorted), how would one determine if any 3 (or more) of those ...
20
votes
4answers
363 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 ...
2
votes
1answer
148 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. ...
6
votes
2answers
2k 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 ...
-3
votes
2answers
234 views

How to correctly call a Module within another Module?

How to embed and call the correct embedded modules inside another Module? Say: ...
6
votes
3answers
551 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 ...
-1
votes
1answer
396 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 ...
17
votes
2answers
365 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 ...
5
votes
2answers
160 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 ...
1
vote
1answer
191 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 ...
1
vote
1answer
91 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: ...
2
votes
3answers
470 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
326 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
0answers
221 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 ...
0
votes
1answer
103 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 ...
7
votes
2answers
2k 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 ...
1
vote
0answers
84 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}} ...
1
vote
1answer
550 views

Matrix Multiplication Modulo 2

I would like to perform matrix multiplication modulo 2. Hence, instead of the usual: A.B I did: ...
0
votes
1answer
165 views

How to generalize a formula?

This is a piece of my code: ...
0
votes
1answer
129 views

Bad function evaluation

I am having some trouble with this function ...
3
votes
4answers
221 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 ...
2
votes
1answer
127 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 ...
1
vote
3answers
612 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 ...
2
votes
1answer
236 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 ...
2
votes
2answers
101 views
1
vote
2answers
148 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 ...
4
votes
3answers
184 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. ...
7
votes
3answers
321 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 ...