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

How can I export my learned ClassiferFunction[] and PredictorFunction[]s?

Mathematica 10 provides beautiful high-level machine learning functionality. Sadly, the learned functions once created are rather opaque objects. I need to use them in other projects, so here is my ...
2
votes
1answer
65 views

Why is either Listable or HoldFirst not working?

Starting with a fresh kernel, I evaluate the following: ...
18
votes
2answers
971 views

Best practice of passing a large number of parameters to functions

I have a number of functions which all take a large number of parameters. I am wondering which is the best practice of passing these parameters to those functions. I could of course simply specify the ...
0
votes
2answers
45 views

Defining a function from a list [closed]

I have a list of 49 numbers, and I would like to define a function f such that f(n) is simply the nth number in the list. How can I do this?
3
votes
1answer
50 views

Manipulate inside a function definition slows down when passed a large data set

I'm working with large data sets (>1gb) which I'm visualizing with Manipulate[]. The following example works fine for me with no additional memory overhead: ...
3
votes
2answers
48 views

Recognizing special cases of a defined function for permuted arguments

I have a number of ugly ways to implement the following pattern recognition task, but I'm looking for something elegant to keep my notebook tidy. I need to define a function of six variables ...
0
votes
2answers
53 views

Eigenvectors inside a maniupulate function

I am trying to make a physics application and it requires that the eigenvectors be taken for a 3x3 matrix. It is my intention that Mathematica never have to deal with the matrices symbolically. ...
0
votes
1answer
61 views

multiple generators for iterative construction of fractal

The code below is an attempt to use more than one generator (in this case two) to generate a fractal using the standard iterative procedure involving generators. Only the first two stages of the ...
4
votes
3answers
373 views

What is the right way to write a function that includes other functions?

I want to include multiple sub functions: $b(x) = 10x$ $w(x) = s + b(x)$ $c(x) = x^2$ into two main functions: $ua(x) = w(x)-c(x)$ $ub(x) = w(x)-c(x)^2$ As a reaction to this answer, I wrote ...
2
votes
2answers
71 views

Adding a tree to a list of trees within a loop [duplicate]

I'm trying to write a function which for a list of trees checks if a given tree is isomorphic with any of the listed, and if not - adds it to the list. I tried to use both ...
9
votes
3answers
108 views

Implement settable properties

I am looking for advice on implementing the following. (It is possible that the answer is that it is simply not a good idea to do this.) I would like to have an object similar to ...
1
vote
1answer
41 views

Overloading ClearAll[] to take no arguments

I use the command ClearAll["Global`*"] so much that I thought it would be nice to have a function that called it. Since by default ClearAll requires an ...
2
votes
1answer
46 views

Having variables appear aqua in user defined function [duplicate]

In many of the inbuilt mathematica function, variables appear aqua like in: However, when creating your own function, variables no longer have that green coloring and are instead colored like an ...
1
vote
1answer
35 views

Q-Multinomial Coefficient [closed]

How can one compute the q-Multinomial Coefficient as a function of q,m and a list {n1,n2,n3,...} in Mathematica? See http://mathworld.wolfram.com/q-MultinomialCoefficient.html for the definition.
4
votes
1answer
105 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 ...
3
votes
3answers
154 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 ...
0
votes
3answers
93 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
56 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
33 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: ...
1
vote
1answer
54 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
71 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
77 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
57 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
184 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
278 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
121 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
52 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
51 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
219 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
44 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
224 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
80 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 ...
3
votes
3answers
230 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
78 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
50 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
35 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
196 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
82 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
20 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
52 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
83 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
55 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
143 views

Multiplicative partition function

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

Defining the function of list of parameters

I'm defining the functions in the following way ...
8
votes
2answers
152 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
43 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
280 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
341 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, ...