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

4
votes
2answers
258 views

Can Mathematica functions have static variables?

In C, one can have variables that are declared static. These are not re-initialized every single time a function is called. ...
4
votes
2answers
758 views

How to define a polynomial/function from an array of coefficients?

I have the coefficients of my desired polynomial in an array CoefArr (I'm new to mathematica, so I think of everything as arrays, it is actually a list I believe) starting with the constant at index ...
4
votes
2answers
161 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 ...
4
votes
3answers
164 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 ...
4
votes
3answers
169 views

How can I combine two Pick expressions into one?

I want to speed up my code, i.e., replace Select by Pick. I think using two Pick ...
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 ...
4
votes
3answers
235 views

Problems using hand-made transformation functions

I experience difficulties when using custom transformation rules. Below I give two concrete problems. I am interested in fixing each of them and in understanding what is wrong with my attitude to the ...
4
votes
2answers
157 views

How to only work on sublists with non-zero (or positive) values

i have a large compositional dataset which contains non-zero and zero values. here is a sample: ...
4
votes
2answers
155 views

How to achieve Set+Part like behaviour in custom Set function?

So I've been toiling away on my DataFrame package. I've been trying to get Set to work with it, but it seems it either can't be done (ie via ...
4
votes
3answers
499 views

Integrating with multiple indicator functions

I am trying to calculate an integral involving multiple indicator functions, such as: $$ h(u,v,w) = -\int_0^1 J^{\prime\prime}(s) (I_{(0,s]}(u) - s)(I_{(0,s]}(v) - s)(I_{(0,s]}(w) - s)\, ...
4
votes
2answers
178 views

Calling a function an unspecified number of times

I would like to be able to call a function an unspecified number of times. That is, I would like the generalization of something like: ...
4
votes
1answer
104 views

Multiple down-values with exactly the same lefthand side

I was working on an answer to this question, when I was sidetracked by what I bring up here. I wanted a function, which when given a string representing a binary number, would convert the string to a ...
4
votes
2answers
223 views

How can I create a class of matrices programmatically?

I am trying to figure out how to create a function that can create the matrix pattern below, where: f[1] returns {{1}} ...
4
votes
4answers
199 views

Speed up replace all

I have large list of rules, I would like to use list/.rule, but assume that rule is a set of rules rule 1, rule2 rule...I want ...
4
votes
2answers
252 views

Returning Replacement Rules in a function and using it later on

I would like a function which returns replacement rules as some internal mathematica functions are doing and use the results in other functions. I could not find any information on that and I'm ...
4
votes
1answer
153 views

Implementing Bilinear Products

Consider a real, vector space $V$ with basis $B=\{v_1,v_2,\dots\}$, and let $\star:V\times V\to\mathbb R$ be a bilinear product on $V$. I would like to implement this product in Mathematica by ...
4
votes
2answers
86 views

Creating functions from output of other calculations

Apologies in advance if the title is vague, I'm not really sure what to call this. I have a function (call it 'foo') that generates a largeish polynomial, and it is natural to make the variables be ...
4
votes
4answers
292 views

Differentiating space curves

I'm trying to do some very basic differential geometry of space curves. For example, a space curve $\gamma:\mathbb R\to\mathbb R^3$ has unit tangent and normal vectors given by ...
4
votes
1answer
257 views

How do I use the result of Solve in a function definition?

I'm trying to adapt the following piece to the part where PowerF is a function of amax. ...
4
votes
1answer
157 views

Find lengths of contours in a ContourPlot

I am trying to find the lengths of different contours in the following plot: It is a complicated piecewise function evaluated on the unit disk. I am hoping there is an easy, generalized way to ...
4
votes
1answer
166 views

Feedback on my Subscript code

I have written a code, and I need a little feed back.. I want to make the Subscript distributive across (1) addition and (2) multiplication to mimic short-hand for ...
4
votes
1answer
225 views
4
votes
1answer
172 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 ...
4
votes
1answer
195 views

What's the most intelligent way to store the information of a selfmade graph?

I have a very long list of element, say $a1,a2, a3$,..., and they are all connected in that each is the child of many parents. E.g. $a74$ is associated with $a2,a55,a71$, and also contains ...
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: ...
3
votes
3answers
312 views

Alternatives pattern in a function definition

I thought that I remembered a way to write a function definition using "|", but can't find documentation for it. So for example, I'd want to define: ...
3
votes
3answers
225 views

Tracking iterations of a function

I'm learning MMA with Wagner's book. On page 41, he uses this to display the iterative steps of a FindRoot. However, I can't seem to replicate this (code at end), perhaps due to the difference in MMA ...
3
votes
3answers
296 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 ...
3
votes
2answers
342 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. ...
3
votes
4answers
216 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 ...
3
votes
4answers
154 views

Pack Solve results into a vector

I am currently using a really easy function to get the eigenvectors of a corresponding eigenspace: ...
3
votes
3answers
195 views

Prime factor counting function

Is there any way I can speed up this prime factor counting function? (I am looking for all numbers in a range with 3 prime factors (counted with multiplicity).) ...
3
votes
2answers
173 views

How to declare a function that uses local variables of a module outside this module?

I have the following code Module[{f,x=0}, f[t_] := x + t; ..........; ...f[1]...; ..........; ] My aim is to put the declaration of the function ...
3
votes
1answer
190 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 ...
3
votes
3answers
152 views

Function that prints a different value than it returns

I want to create a function something like this: test[x_] := (Print[1]; Return[2];) Except it prints 1 when executed as ...
3
votes
2answers
227 views

Differential operator squared

I defined a one-dimensional momentum operator $\hat{p}=-i\hbar\frac{\partial}{\partial{x}}$ in Mathematica p = -I * h * D[#, x]& and I want to get the ...
3
votes
2answers
310 views

Why does the first derivative of a piecewise continuous function have discontinuities?

I have this piecewise continuous function which is also continuously differentiable over time : ...
3
votes
4answers
212 views

Defining a pure function involving a function argument

I've got an expression involving x and y that I want to use as a MeshFunction: ...
3
votes
2answers
142 views

Multiplicative partition function

I am trying to create a multiplicative partition function that would generate something like ...
3
votes
1answer
174 views

Extracting function calls from an expression

I have an expression like this: expr = F[0, 1] + 2 F[0, 0] - 17*F[2, 0] and I want to transform it to a list consisting of all function calls with its arguments: ...
3
votes
2answers
477 views

Ways to plot interpolating functions more economically?

I am interpolating a lot of data over geographic coordinates obtained via GPS, and frequently I need to plot these interpolated functions together. For example, I may have an interpolated elevation ...
3
votes
3answers
132 views

How to identify patterns in a tensor-polynomial and replace appropriate symbol?

I want to write a function tensorReplace[input] that takes a tensor polynomial in $r_i$ such as $r_i r_j r_k+r_i q_j q_k +q_i q_j q_k$ , and replaces each monomial ...
3
votes
2answers
145 views

Apply UpValues before Listability

I'm trying to modify Plus but am running into trouble with it being Listable: ...
3
votes
1answer
111 views

String pattern matching in function definition

I have a function f and its downvalues. I want to code an extra downvalue for arguments of the form "part1:part2" I don't understand why ...
3
votes
1answer
94 views

How to Override the Head of a Symbol

I was wondering if it is possible to override the head of symbols/operators. As a motivating example if I run: Then Ket will be returned. But lets say I want Bob to be returned instead of Ket. Is ...
3
votes
1answer
222 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, ...
3
votes
2answers
101 views

How to apply multiple/complicated requirements for a pattern in a function input [duplicate]

In this question. I suggested that to test whether the input of a function was a matrix of numbers one could create the following test: ...
3
votes
3answers
152 views

Function[] argument nesting for operations like Select[]

The following code selects a special element from a list thelist = {"this", "notthis"}; Select[thelist, (# == "this") &] What I want is a function which ...
3
votes
1answer
178 views

Defining the Moyal Product in Mathematica

What would be the best way to define the Moyal Product, $f(x,p)\star g(x,p)$, in Mathematica? $f(x,p)\star g(x,p)$ may be written as $f\star ...