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

0
votes
3answers
97 views

Using “With” outside a function on its parameters [duplicate]

I am making complex 3d graphics, and to clear up functions, I want to use With to remove a repeated action. This action occurs many times through many functions, so ...
1
vote
1answer
64 views

Why is Return not forcing an exit from my function? [duplicate]

Here is simplified version of my code that exhibits the behavior troubling me: ...
0
votes
1answer
40 views

Assigning a given value if a function returns an error [duplicate]

This question has to do with error handling, I guess. I am using Brent's Method in order to numerically find the root of a function. This is just an example of what I am doing: ...
2
votes
1answer
90 views

Define PolyLog so that positive reals evaluate on upper edge of branch cut

Generalizing the previous question: Define Log so that negative reals evaluate on lower edge of branch For real positive values $x>1$, Mathematica's polylogarithm function ...
1
vote
2answers
74 views

Returning ancillary computation results from a function

Suppose I want to implement a function to solve a very basic Elliptic problem using Finite Element of some degree (It's just an example). Normally the user of my function is only interested to the ...
0
votes
1answer
80 views

Defining a function that differentiates another function with rule replacement

This question is related to the question I posted here. In that question, I thought I have simplified my original problem into an equivalent concise version, but I found that it is not. So I decided ...
0
votes
0answers
58 views

How can I avoid a scoping problem when differentiating?

I simplify my real problem as follows: I define a rule function as Clear[rule]; rule[i_] := tt -> i; the real rule function is not this simple. I define a ...
3
votes
1answer
196 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 ...
13
votes
1answer
327 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 ...
3
votes
1answer
143 views

Gateaux (directional) derivatives and higher order differentials of a functional

I would like to calculate the Gateaux derivative of a functional (i.e. a function depending on functions). A simple example for the Dirichlet functional: $L(u(x))=\int_0^1 \frac{1}{2} (u'(x))^2 dx$ ...
0
votes
0answers
54 views

Maximum and mean of a multi-variate function over a restricted lattice

I have a function Q, and I want to find the maximum value, the corresponding arguments for the maximum value, and the mean over a certain range of arguments. I ...
1
vote
2answers
56 views

Creating function with matrix argument

I am completely new to programming, so I apologize in advance for a stupid question. I want to program an algorithm for computing invariant factors of a finitely generated abelian group, so in ...
3
votes
4answers
231 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: ...
0
votes
1answer
51 views

Alternatives to indexed variables in Compile

According to the excellent answer given here by @Leonid Shifrin: How to compile effectively?, You can not really use indexed variables in Compile, although it ...
4
votes
2answers
232 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}} ...
0
votes
2answers
85 views

Defining a function as a sum of several Gaussians

I have a function that is a summation of several Gaussians. Working with a 1D Gaussian here, there are 3 variables for each Gaussian: A, ...
2
votes
1answer
65 views

Defining a function (but not explicitly)

I have a question that must have a simple answer, but googling and searching this website did not produce an answer, so I'm asking it here. I'm working with three variables $x, y, \theta$, and I want ...
2
votes
3answers
259 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
2answers
79 views

Function pattern to support listed and non-listed arguments

I often want to write functions that take as an argument either a) a rule or b) a list of rules. As an example, when using Replace with just one rule, it does not ...
2
votes
0answers
17 views

In a Function, how to Ensure Code Runs Prior to an Abort [duplicate]

Is there a way to set aside some code in a function that will be triggered to run if the evaluation of the function is aborted by a user? Specifically, I have a function that has to open a log file to ...
2
votes
1answer
78 views

Defining a rule with functions of arbitrary arguments

I have a set of "functions operators", say A1[u],A2[u],A3[u],and I would like to define a rule such that, for instance, (x*A1[u1]).(y*A2[u2]) returns ...
1
vote
0answers
38 views

A more efficient way of fitting a non-linear model that returns an entire list?

I have a function that takes two parameters, del and nu, and returns a list npoints long ...
5
votes
1answer
199 views

Procedural to functional style

The following code generates an iterative mapping of some trigonometric functions. I have a couple questions about it. The first question is how to optimize this iterative process using mathematica's ...
3
votes
1answer
100 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 ...
0
votes
0answers
21 views

Coding contour integration [duplicate]

I am struggling to understand contour integration. How would it be applied in Mathematica? I am hoping that if I see it written in Mathematica code, it will help me understand it. eg how would ...
-1
votes
1answer
53 views

Creating new functions using the names of existing ones

I have a list of functions {G[x_, y_], F[x_, y_], C[x_, y_]} how I can create the list of new functions such as ...
1
vote
1answer
85 views

Symbolic multiplicative partitions

Let $p_n\#\equiv\prod_{k=1}^{n}p_k$ (primorial): p[n_] := Times @@ Prime[Range[n]] then the multiplicative partitions of $p_{1,2,3,4}\#$ are $$ \{\{2\}\},$$$$ ...
0
votes
2answers
56 views

Define functions from two lists

I have two lists {G[x_,y_],F[x_,y_],C[x_,y_]} and {x*y-x, 3x+y, x^y} Now I need to define 3 functions ...
3
votes
2answers
149 views

Multiplicative partition function

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

Defining the function of list of parameters

I'm defining the functions in the following way ...
8
votes
2answers
156 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: ...
0
votes
2answers
45 views

how to define roots of an equation as a function? [closed]

I need to solve an equation depending on 3variables in only one variables and define the roots as a function of the remaining variables? I used some thing like that: ...
2
votes
3answers
303 views

Generating list of alphabet letters

I want to create a list of length n with the letters of the alphabet. For example, if n = 3, by list would be ...
10
votes
2answers
352 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
0answers
67 views

Maximum number of literal values for SetDelayed?

Under what circumstances should I expect Mathematica to turn a delayed assignment into an immediate assignment? I have put together a minimal example (available here) which demonstrates this behavior. ...
1
vote
1answer
82 views

Requiring NumericQ in Pure Function

Potential duplicate, but doesn't seem to solve my problem: Using patterns in pure functions I have a function that returns an InterpolatingFunction, and it works ...
1
vote
0answers
64 views

How can I add options to functions [closed]

One wish take easy advantage of the packages/codes buildt through our engagement with mathematica. Through building packages I see that I have made several functions which are simple and closely ...
0
votes
1answer
62 views

function of derivative of a function

I know a similar question is answered somewhere, but it doesn't work for my case for some reason. I have the following problem in Mathematica. I want to compute f[h[x]]= x^2 + D[h[x],x]. Here, h[x] ...
0
votes
1answer
40 views

Defining arbitrary order derivatives function [duplicate]

I have a function $\phi(t)$ and I want to define a function $\phi D(t,r):=\phi^{(r)}(t)$, in other words evaluate the $r$'th derivative of $\phi$ at $t$. The naive code that I have is: ...
0
votes
1answer
48 views

Generating list of functions from list of expressions

I know how to create a function from concrete expression like Function[{x, y}, x^2 + y^2] But what if I have a list of expressions list1 and I know the set of ...
5
votes
3answers
105 views

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

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

How to assign special colors to the output of DensityPlot?

I have created the following function for plotting ...
4
votes
3answers
418 views

Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
4
votes
4answers
207 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 ...
2
votes
2answers
94 views

Handling a sequence of unevaluated arguments

I have the following piece of code: ...
0
votes
1answer
81 views

Define a function that returns a function with a variable number of arguments

Previously, I've defined a function with a variable number of arguments with the following method. ...
1
vote
1answer
137 views

Speeding up a function

This is un updated version of a previous (now deleted) post of mine. I need to marginalize a distribution. I was suggested to use the code in this thread. This works just fine, but the problem is ...
2
votes
5answers
264 views

Returning all positions for all different occurences of elements in a list

Is there way to construct oneliner as pure function(s), so that I enter mylist only on one place - on the end of line. And that function return the same result as ...
0
votes
0answers
78 views

How to save the results in an identical Excel file (.xls) in each iteration of a Do loop

Assume the following code (Below code is just an example to clearly illustrate my problem): ...