A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Questions tagged [function-construction]

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.

Filter by
Sorted by
Tagged with
20
votes
1answer
989 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 ...
19
votes
3answers
570 views

Improving speed of code computing number of nonrepeating partitions

I need to answer the following for a number of parameters: How many ways can the integer $k$ be written as a sum of $n$ different integers ranging from $1$ to $m$? My initial attempt was the ...
19
votes
4answers
569 views

Skipping “middle” Optional Arguments

Is there a way to call a function where the "middle" arguments should use their default value while specifying values for the "right-most" arguments? For example, given this function: ...
19
votes
2answers
466 views

Clean and handy Options management/filtering

the case I want to be able to create a function with some default options but also without need to add full explicit list of options available for it. And then inside I want to be able to filter ...
19
votes
1answer
365 views

What is the difference between defining a function and specifying the type of argument, versus applying a test to that argument?

Say I want to create a function that evaluates differently based on what type of argument is given. I've found two ways of doing this, ...
19
votes
1answer
472 views

How to make a function like Set, but with a Block construct for the pattern names

How can we define a function that works like f[x_]=ComputeSomething[x] and treats x as a variable that does not have a value? ...
18
votes
5answers
2k views

Picking random items out of a list only once

I'm trying to create a function that randomly returns a value from a list but remembers the values that have been given before. At the end when the list is empty it should return an empty list. ...
18
votes
4answers
2k views

Cleanest way to take a[b[c]] to a[b][c]

As indicated in the title I'm looking for the fastest way to transform a[b[c]] into a[b][c], and the natural generalization to ...
18
votes
3answers
3k views

Passing down arguments

In R, one can use ... to pass arguments down to another function. For example ...
18
votes
2answers
227 views

Would it be a good idea to use Failure and FailureQ in own code

Since version 10 (at least 10.0.2 but I think 10.0.0) there is Failure and since version 10.2 there is also FailureQ. As often ...
18
votes
1answer
271 views

Why Function is not identical to `&` in autocompilation?

Here is the example, copied from here square = Function[x, x^2]; square1 = #^2 &; the timing and unpacking status shows ...
17
votes
6answers
1k views

How can I evaluate only a single step of a recursive function?

Let's say have a simple recursive function for the Fibonacci sequence f[0] := 1 f[1] := 1 f[n_] := f[n - 1] + f[n - 2] but I want to see how it will expand in a ...
17
votes
3answers
363 views

Is it possible to change the order that symbol definitions are checked? (DownValues vs UpValues)

Suppose I have the function f with the following definition assigned to it: ...
17
votes
2answers
3k views

Stereographic Projection

Say I want to represent points of the complex plane in the sphere $\Bbb S^2$ using stereographic projection. That is, the Riemann sphere: Specifically, it would be nice to be able to: Given the ...
16
votes
7answers
959 views

Permutations of nested parentheses (Dyck words)

How would I construct a function that outputs Dyck Words? e.g. - there are 14 words in $\mathcal{D}_{8}$: ...
16
votes
7answers
2k views

A function that accepts a pair or a list of pairs

Probably a duplicate, but it's not easy to search on "lists". I have a function that accepts a list of lists, say {{a1, b1}, {a2, b2}, {a3, b3}} and performs some ...
16
votes
4answers
503 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 $f(a,b,c;...
16
votes
2answers
1k 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, ...
16
votes
2answers
314 views

Improving Performance of an XY Monte Carlo

I normally write my Monte Carlo codes in Fortran for speed, but I was doing some quick and dirty work and wrote one in Mathematica for the XY model on a square lattice (see Kosterlitz-Thouless ...
16
votes
2answers
507 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 ...
16
votes
4answers
826 views

Is it possible to get the order of inputs when “overloading” an orderless function?

When working with symbolic matrix operations or other objects which don't have commutative multiplication, it would be nice to not have to constantly switch out times for ...
16
votes
1answer
544 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 ...
16
votes
1answer
429 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 ...
15
votes
6answers
2k views

How can I plot this spirally surface?

I would like to plot a surface that looks something like this sand spiral. But I really have no idea how. Ideally I would like to be able to express this as a function in cartesian coordinates. I'm ...
15
votes
3answers
2k views

What is Mathematica's equivalent to MATLAB's filter function?

The MATLAB code filter(0.5,[1, -0.5], [1:10]) is equivalent to Rest@FoldList[(#1 + #2)/2. &, 0, Range[10]] I don't ...
15
votes
4answers
11k views

Turn expression into function

I'm new to Mathematica and I spent the last hour trying to make the expression ...
15
votes
3answers
297 views

What's wrong with this pattern of the form Except[Repeated[…]]?

I'm trying to build a head that recognizes molecules. Here's the code ...
15
votes
6answers
3k views

Create a polynomial of a given degree

In Mathematica, how can I create a polynomial function in given variables of a given degree with unknown coefficents? That is, I am looking for a function ...
15
votes
2answers
2k views

Constructing functions with variable number of output arguments

In Mathematica, you can construct a function f to have different definitions based on its input arguments. For example, ...
15
votes
3answers
1k views

How to define a function that is Listable for some but not all arguments?

I want to define a function f[x,y] that is Listable for only the first argument, not the second. If we use ...
15
votes
1answer
342 views

Why do subsequent evaluations seem to be stopped by a False Condition (/;)?

Exploring another problem and this example of @halirutan, I came across an evaluation behavior I don't understand. When g[] below is evaluated the first time, <...
15
votes
1answer
644 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: ...
15
votes
1answer
860 views

How can I define a Listable function only apply to a vector?

Consider the function f which should behave as f[{{1, 2}, {3, 4}, {5, 6, 7}}] ...
15
votes
3answers
451 views

Another difference between Set and Setdelayed. Evaluation shortcut?

A little while ago I wondered why f[x_] = f[x] gives an infinite iteration. I ended up discovering the following difference in evaluation between ...
14
votes
5answers
2k views

define a function from a list

I would like to create a mapping from a list and I am wondering if there is a slick way of doing it. For example, consider the list ...
14
votes
2answers
3k 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: ...
14
votes
4answers
1k views

How to check the style and number of arguments like the built-in functions?

Given that I have a function sqrt[x] that I must check the style and number of argument x. I can do like this: ...
14
votes
2answers
3k views

How to create 'help' `?` for a function (as documentation)

I have defined a function foo 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 used. ...
14
votes
2answers
738 views

Flatten not working

I am trying to modify some code proposed here Part assignment is not a symbol and I cannot manage to make Flatten work. I write ...
14
votes
2answers
453 views

How I can define this operator in Mathematica?

I want to define an operator $G$ such that $$G(f):=\begin{cases}f(\{x\}),&\lfloor x\rfloor\text{ is even}\\\frac1{f(\{x\})},&\lfloor x\rfloor\text{ is odd}\end{cases}$$ for any function $f$, ...
14
votes
2answers
402 views

Named patterns in Except

Bug introduced in 8 or earlier and fixed in 10.4.0 Bug isn't present in version 5.2 I don't think I quite understand how Except works. I want to define $f(a,b) = ...
14
votes
2answers
2k views

Converting ConditionalExpression to Piecewise

I have Solve[] return a list like ...
14
votes
3answers
917 views

Pass by reference for an option argument

Pass by reference can be faked by using HoldAll or something similar in the definition of a "Function". But can pass by reference be faked for symbols passed as ...
14
votes
1answer
363 views

How to define homogeneous functions that respect Euler's theorem of homogeneous functions?

A homogeneous function of degree $k$ is defined as a function that observes the following specification rule: $f(a x_1, a x_2, ..., a x_n) = a^k f(x_1, x_2, ..., x_n)$ (see Wikipedia entry, ...
13
votes
4answers
899 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 ...
13
votes
5answers
8k views

Make a differentiable smooth sawtooth waveform

So, I am trying to understand the effect of sawtooth waveform on a mechanical system model. For example, a system of a plate and a box on it. And then we try to oscillate the plate. In order to do ...
13
votes
4answers
5k views

Simulate MATLAB's meshgrid function

Here is MATLAB's meshgrid. I came up with these implementations in Mathematica: ...
13
votes
4answers
436 views

Renaming options of custom functions while preserving backwards compatibility

I have a package with many functions. There is an option name that several functions share. I feel that the current name of this option was not the best choice. I would like to rename it, but at the ...
13
votes
2answers
13k 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 (\...
13
votes
3answers
7k views

“do … while” loop equivalent in Mathematica [closed]

Have I missed something or is there no built-in which mimics the behaviour of do ... while loop? I am looking for a construct that evaluates procedure once and ...