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

1
vote
1answer
85 views

How can I implement a moving standard deviation for a time series?

I imported the following dataset and converted it into a time series. ...
0
votes
0answers
81 views

Defining a function after doing several calculations inside [on hold]

These things are probably very well known but I am a bit confused. After spending a long time, I was able to perform an operation on a function. Let's call the function ...
3
votes
2answers
224 views

Handling options in my function

It seems this should be easy, by I just can not figure out how. I want to have a function that I can pass a list of options to, or use a default. I have found two very similar questions, but I can't ...
42
votes
10answers
8k views

Generating a Sierpinski carpet

I am trying to draw a Sierpinski_carpet. I have code that works, but I think there is a more elegant way to do than my way. Maybe I couls use Tuples or ...
0
votes
1answer
34 views

Call user defined function from many notebooks [duplicate]

I wanted to ask a simple question. I am new to Mathematica, and I don`t find a way to call user defined functions just as I call Mathematica functions. I want to use my own defined functions, but ...
0
votes
0answers
39 views

Creating a module consisting of i variables [on hold]

I'm sitting with 2 different, yet similar problems. I am trying to make a function, giving the user the freedom of quantity of variables. First it'd be a simple: (example) ...
1
vote
1answer
78 views

Double arguments and place of the underscore

Could someone explain me of this bit of code below is correct: Is it possible to define a function with a second bracket, see below, i.e. f[x_][y_]? And what does ...
7
votes
6answers
236 views

Combinations of multiple matching patterns

I wonder if there is any nice way to combine DownValues (or any other suitable rule-/pattern-/function -based Mathematica construct) when multiple patterns match an expression. Let me explain what I ...
5
votes
2answers
69 views

How to define a complicated function inside the body of Compile?

I want to compile a function in a way to keep its memory footprint down. In the example below, I am trying to compile a function f that makes three calls to ...
3
votes
3answers
114 views

General function for the expansion of a polynomial of operators

This question is motivated by a Quantum mechanical problem - but in explaining the problem - I assume no knowledge of quantum mechanics. I want to define a function that can expand and simplify the ...
1
vote
2answers
72 views

What's the easiest way to remove overall factors from polynomials?

I have code that outputs polynomials (in #1) such as the ones below (note that the trailing Function character ...
9
votes
1answer
128 views

Maximizing a recursive functional equation

I would like to maximize a recursive functional equation but I am struggling with setting up the problem correctly. The equation I am interested in captures the nature of a decision process over time ...
0
votes
1answer
44 views

Simple grouping equations [closed]

I'm new in the Mathematica world. A very basic question. I have been using the command Eliminate on a set of equations as follow: ...
1
vote
2answers
39 views

Evaluating While Loops inside Modules [duplicate]

I am trying to compute some objects inside while loops within modules, but I keep getting the following error: ...
1
vote
1answer
40 views

Construct all possible sets of pairs [duplicate]

I am looking for a nice way of constructing all possible pairings of a given list. Currently I am using the brute force approach of sieving the 2-partitions of all permutations. ...
1
vote
1answer
105 views

The basic primitive recursive functions

How can the basic primitive recursive functions be expressed in the Wolfram Language? Apparently here's an example for primitive recursion: ...
0
votes
1answer
94 views
0
votes
1answer
45 views

How to define InterpolatingFunction on a variable set of points?

I would like to define an InterpolatingFunction to which data points are transferred as a parameter, e.g.: ...
22
votes
4answers
311 views

Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)?

For various reasons related to workflow associated with creating figures for journals, I am creating functions that will accept graphics primitives and a set of options and spit out a ...
4
votes
2answers
159 views

Subset-type function construction

This is probably a very straightforward thing I am missing, and probably also a duplicate(!), but I can't for the life of me think how to do this cleaner. I want to achieve what ...
4
votes
2answers
47 views

Is it possible to match a sequence with multiple heads without using Condition

Suppose I have: expr = Times[2, a[x,r], a[y,1], c[z,0,0], d[w,m]]; If I want to match a sequence of expressions with the same head ...
4
votes
1answer
86 views

Pertubation series of given equations

I would like to define a function that does the following. If I have an equation, for example: $$f(x)+[1-f^2(x)]+f^{\prime\prime}(x)+f(x)\ f^\prime(x)=0$$ and I am given a value for $f$ to expand ...
0
votes
0answers
73 views

How to change the built-in function? [duplicate]

As you can see,the original Differences will get {} when your parameter is a list with one element just. Differences /@ {{1, 2, ...
4
votes
2answers
73 views

Finding the smallest starting number producing a Collatz sequence of length 125 which ends with a value > 4

I'm supposed to do a Mathematica function that selects the smallest number not less or equal to 4 after 125 iterations. I am not good at coding, so some help/advice would be awesome. My code so far. ...
0
votes
1answer
47 views

Cumulative function definition and plotting [closed]

I'm quite new to Mathematica. I have an issue defining cumulatively a function. Let's say I have a waveform f[t] , and I want to create a function with many copies ...
0
votes
1answer
59 views
4
votes
1answer
134 views

How to efficiently write code with many functions that have variable number of iterators/arguments/

I am finding myself having to code functions like this: $$\text{answer} = \sum_{k_0 = 0}^{\color{Blue}n_0} \sum_{k_1=0}^{\color{Blue}n_1}\cdots\sum_{k_{N-1}=0}^{\color{Blue}n_{N-1}} ...
31
votes
4answers
2k views

Using pure functions in Table

I need a table with the elements made of pure functions and list elements. This is a simplified example: I need a list as: ...
-1
votes
2answers
64 views

Gradient of interpolated 3D data in mathematica 9

tt = Flatten[Table[{x, y, z, btot[x, y, z]}, {x, -1, 1, 0.1}, {y, -1, 1,0.1}, {z, -1, 1, 0.1}], 2]; ff = Interpolation[tt] Till here it is working fine as it is ...
4
votes
2answers
77 views

How to Return $Failed symbol in a MakeBoxes context?

Three months ago, I asked a question about How to build a function object like the built-ins? Today, I used a solution proposed by István Zachar ...
8
votes
4answers
234 views

How to inject unevaluated list of Unique into a RuleDelayed?

I am writing a function that constructs a rule which has Unique[] in the RHS. Here is a simplified segment of code ...
0
votes
1answer
46 views

Results of a function are different when the function call and subsequent statements are evaluated one line at a time

I have some code that I am porting from VBA to Mathematica. The original code processes data on an Excel spreadsheet. In Mathematica, I find it convenient to replicate the Excel spreadsheet data ...
7
votes
1answer
126 views

Can one specify a “computed optional value”?

I would like to write a function with an optional argument, which if absent should not be a default value, but rather a computed value. What I mean is the following [warning this code does not work, ...
8
votes
1answer
84 views

How to make `D` apply chain rule on user-defined symbols as it does on `Dot` and `NonCommutativeMultiply`

I have my own version of Dot called mydot, and it behaves quite similar to the built-in version in that it represents the ...
6
votes
3answers
240 views

How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the ...
1
vote
2answers
108 views

How to make function accept list of rules as an argument?

Considering the list of rules: r= {"a" -> "1", "b" -> "2", "c" -> 3, d -> 231, "e" -> 1.25}; I want to have a function ...
10
votes
4answers
177 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 ...
3
votes
1answer
62 views

Finding all Spanish words with given vowel patterns

I'm trying to implement a function using DictionaryLookup that does the following: Uses a Spanish dictionary Lists and counts of all words where the 5 vowels ...
2
votes
1answer
34 views

Expanding/printing function with constrained arguments [duplicate]

Let's say you have a function $ f(t) = t, \text{ for } t > 0,$ declared in Mathematica as f[t_?(# > 0 &)] := t; How do you print that function? I ...
1
vote
1answer
87 views

Continued fraction with different numerators [duplicate]

In Mathematica, is it possible to form the continued fraction of the following formula? $$ f = a_1+\cfrac{b_1}{a_1+a_2+\cfrac{b_2}{a_2+a_3+\cfrac{b_3}{a_3+a_4+\cfrac{b_4}{a_4+...}}}} $$
8
votes
4answers
194 views

Identify the independent variable in an expression [duplicate]

Suppose that you write something like this for your students. This is just the beginning of something I would like to write to help them with the formal definition of a limit, but I am puzzled by the ...
22
votes
3answers
472 views
2
votes
2answers
111 views

Plotting a function defined to be infinite over a sub-domain [duplicate]

I'm trying to plot the following function with the constraints shown, $$f(x)=\begin{cases} \infty \quad x<0 \\ A\,x\quad x\ge 0 \end{cases}$$ However I can't work out how to show this in ...
7
votes
4answers
96 views

Problem with handling a factor of $1$ in pattern matching

I am trying to implement the creation and annihilation operators $\hat{a}$ and $\hat{a}^{\dagger}$ of quantum mechanics with the following code, but alas, there is a problem with the pattern matching ...
6
votes
2answers
859 views

Number of digits for factorial of 12345678987654321

A hard math problem What is the number of digits (IntegerLength) of the factorial of 12 345 678 987 654 321? The number of zeros at the end of this factorial was calculated and it is huge: exactly 3 ...
5
votes
1answer
119 views

How to make my fuction return a step count

I am new to mathematica, and I have this 3d random walker program which runs with a fixed (given) amount of steps. What I want it to do is to run forever but stop when two walkers collide (aka have ...
6
votes
3answers
82 views

How to organize expression by symbols (like Collect), but apply different functions to each coefficient

I want to mimic the functionality of Collect[expr, {vars}, func], but with the following modification: The function f that is ...
11
votes
5answers
799 views

Reverse Polish Notation

Is there a way to evaluate a string containing RPN in Mathematica? SE thinks this question is too short, so let me expand on it. Do you know of any function, that provides the following ...
2
votes
1answer
101 views

Pattern condition involving multiple arguments to a function

I can define conditions on individual arguments to a function: f[a_List/;condition(a),b_List/;condition(b)]:=(...) I am wondering if there is a way of declaring ...
3
votes
1answer
83 views

How to inject segments of code into functions defined by SetDelayed [duplicate]

I need to define a set of functions that are written in terms of common but lengthy expressions. To effect this, I want to inject abbreviations for such expressions into the RHS of ...