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

15
votes
1answer
274 views

Multiple templates for a single user-defined function

How do I use the ::usage tag to allow the Mathematica v9 front end to know that there are multiple ways to call an overloaded function (a function that can have ...
3
votes
1answer
106 views

Is it safe to assign a variable and function of the same name for different things?

I'm writing out a notebook that goes through the van der Waals Equation of State for gases, and I run into a situation where I want to assign (simplified) Tc[b_]:=5b, use that to solve for b in terms ...
-1
votes
1answer
955 views

How can I make a plot of the Higgs potential?

I was wondering, how would could I make a drawing of the Higgs potential aka "mexican hat potential". I am quite new to Mathematica and don't know where to look to learn how to implement such a ...
6
votes
4answers
462 views

Function argument to default under certain condition

Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met? For example, if a function is defined as: ...
2
votes
1answer
210 views

Can my permutations function be defined using iteration?

I have two functions, tuples and perm. They are similar. ...
2
votes
0answers
80 views
3
votes
2answers
151 views

Apply UpValues before Listability

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

How can I define a function that can accumulate its parameter?

For example, write a function add: add[3] return 3 add[4] return 7 add[10] return 17
10
votes
2answers
287 views

Functions that remember their values

Can someone explain what is going on with the following ... func[y_] := func[y] = (Print["Hello world !!!"];) func[1] Hello world !!! ...
1
vote
1answer
345 views

Fitting parameters of user defined function returning discrete values

So, I'm fairly new to Mathematica (apologies in advance for the noobiness), and I'm having some problems regarding fitting the parameters of a user defined function to data. I'll give a representative ...
6
votes
3answers
395 views

How can I rewrite this with NestList or FoldList?

The original code used For: ...
3
votes
2answers
509 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
0answers
52 views

How to influence Suggestion Bar in your package/code [duplicate]

Possible Duplicate: Is it possible to customize the Suggestions Bar? I have written a little code (like a minipackage) of different functions. In using my code, the user typically starts ...
0
votes
1answer
196 views

Creating Recursive Sequences

Quite a simple question, I reckon, however, even quite an extensive search hasn't helped me. I want to define a recursively defined sequence that starts with defined ...
4
votes
1answer
169 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 ...
1
vote
3answers
371 views

More than one expression in the Table[] command?

I would like to construct a Matrix using the Table[] command. The issue is that I would like to use more than one expression for it. What I need to obtain is something like this : ...
2
votes
2answers
194 views

Hiding certain user-defined functions in front-end autocompletion

I have written an elaborate code that contains a collection of functions for the user and a bunch of auxiliary functions whose existence I would like to hide away from the user. I've written my code ...
4
votes
2answers
232 views

Pattern matching repeated structures with named subexpression components

I am trying to write a function with a pattern that tests its arguments for a particular structure. But in that pattern I'd like to name a subpattern to make it easier to exract and operate on. The ...
7
votes
7answers
586 views

Can my color function be written more simply?

The code shown below works well, but I think the pure function I'm supplying to the ColorFunction option could be more elegant. How can I simplify it? ...
3
votes
3answers
134 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 ...
2
votes
1answer
147 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
164 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
261 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
547 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
171 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
569 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
214 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
100 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
365 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
236 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
271 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
385 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
306 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
182 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
214 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
233 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
167 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
172 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
230 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
176 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 ...
19
votes
4answers
355 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
146 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
225 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
519 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
383 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
360 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 ...