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

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
189 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
463 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
323 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
212 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
102 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
520 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
161 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
220 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
557 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
221 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
147 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
179 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
310 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 ...
4
votes
2answers
329 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 ...
3
votes
3answers
329 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 ...
2
votes
1answer
144 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 ...
4
votes
2answers
191 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, ...
12
votes
1answer
280 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 ...
10
votes
2answers
757 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: ...
4
votes
2answers
2k 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 ...
2
votes
2answers
168 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: ...
9
votes
1answer
661 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 ...
12
votes
1answer
252 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: ...
11
votes
2answers
253 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. ...
1
vote
2answers
349 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, ...
8
votes
2answers
403 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: ...
1
vote
2answers
352 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 ...
1
vote
2answers
137 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 ...
1
vote
3answers
845 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. ...
1
vote
1answer
218 views

Calling part of a variable in a user defined function

I have defined a function ...
3
votes
4answers
161 views

Pack Solve results into a vector

I am currently using a really easy function to get the eigenvectors of a corresponding eigenspace: ...
8
votes
3answers
359 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 ...
8
votes
2answers
259 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: ...
18
votes
3answers
364 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: ...
22
votes
6answers
2k 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 ...
3
votes
9answers
1k views

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

I'm trying to solve the fourth Project Euler's problem with this: ...
4
votes
2answers
165 views

Standardizing a coset table via matrix manipulation

Suppose we have a group $G$ and a subgroup $H$. A coset table encodes the permutation representation of $G$ on the right cosets of $H$. When we want to use these coset tables in calculations, it is ...
5
votes
4answers
635 views

Swap two entries in a matrix

Is there a nice, simple script one can use to swap two entires in a matrix? For example, suppose we have the matrix below: $A=\begin{pmatrix}2 & 4 & 1\\ 3 & 1 & 2\\ 4 & 3 & 1 ...
3
votes
2answers
395 views

Defining a function using Series? [duplicate]

Possible Duplicate: General::ivar is not a valid variable when plotting - what actually causes this and how to avoid it? I need to define a function using the output of series, i.e. ...
4
votes
1answer
176 views

Finding mappings between expressions

Suppose we have an expression of the form: $j=\frac{A\left(t\right)}{B\left(t\right)}=\frac{C\left(s\right)}{D\left(s\right)}$ That is, $j$ can be expressed either as a function of $t$, or as a ...
11
votes
1answer
433 views

Lazy lists of Tuples and Subsets

I'm trying to build a lazy list that evaluates the n'th m-tuple or subset of a given list using Mathematicas ordering without calculating all the Tuples. The purpose is to allow for example the ...
3
votes
1answer
205 views

Evaluation of self-defined functions

I defined a function that I call disc which I want to minimize later. The function is defined as ...
11
votes
4answers
469 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 ...