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

5
votes
2answers
61 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 ...
1
vote
2answers
71 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 ...
7
votes
6answers
226 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 ...
9
votes
1answer
121 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
38 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
103 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
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.: ...
4
votes
2answers
46 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
2answers
158 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
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

Should I define a function or not?

I was trying to execute the following code: ...
-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
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}} ...
8
votes
4answers
231 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 ...
4
votes
2answers
76 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 ...
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, ...
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 ...
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 ...
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
193 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 ...
2
votes
2answers
109 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
856 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 ...
11
votes
5answers
798 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 ...
6
votes
3answers
79 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 ...
10
votes
4answers
171 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 ...
2
votes
1answer
100 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 ...
0
votes
1answer
94 views

How to calculate the pseudo-remainder and pseudo-quotient of two multivariate polynomials

I have fixed some code, but it always gives me errors. Can anybody tell me its right code? ...
3
votes
1answer
82 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 ...
1
vote
0answers
41 views

Rescale large numerical factors in rational functions

Given a rational function $$ f(x_1,x_2) = \dfrac{r_1 x_1^2 + r_2 x_2}{r_3 x_1 + r_4 x_2}, $$ with $r_i$ arbitrary real or complex numbers, is there a built-in function to get Mathemtica to rewrite as ...
9
votes
1answer
146 views

How to make a function to analyse a sentence?

I have two lists: grammarNonterminal = {{VP,{V,NP}},{NP,{D,N}},{S,{NP,VP}}} sentence = {{D,"the"},{N,"man"},{V,"hit"},{D,"the"},{N,"table"}} I want to make a ...
13
votes
2answers
273 views

Named patterns in Except

Bug introduced in 8 or earlier and persisting through 10.3 or later Bug isn't present in version 5.2 I don't think I quite understand how Except works. I want ...
14
votes
1answer
216 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, ...
1
vote
2answers
59 views

NDSolve with one parameter

This is a copyable minimal example to illustrate the problem that occours when I include one parameter U into a differential equation which I want to solve with ...
0
votes
1answer
29 views

Solution of a function that is in a variable [duplicate]

If I want to get the solution for a simple function, e.g. a^x, I can use: a = 2; Function[x, a^x][2] However, if I have the function in a variable, I can't get ...
2
votes
2answers
44 views

Deciding whether the return value of a function is defined based on its input [duplicate]

I have been met with the following problem: I have defined the following: myFunc[e_Integer]; That means, if I call: ...
5
votes
2answers
273 views

More elegant function construction for blues loop

Is there a more elegant way to do this: ...
3
votes
1answer
76 views

Evaluate Limit only if both directions(sides) are equal?Ex: $\lim _{x\to 0}\left(\frac{1}{x}\right)=DNE$

I want the limit of a function to only evaluate if both the left and right hand side are equal. For example $\lim _{x\to 0}\left(\frac{1}{x}\right)$ doesn't exist(two sided limit). However, the right ...
0
votes
1answer
76 views

High-order functions and interactive objects

I have been developing Mathematica code to interpret LaTeX code, extract formulas and apply various Mathematica operations (e.g. Simplify, ...
1
vote
1answer
80 views

ClickPane and InputField entry in a Manipulate

By clicking at a point on the slope field for an ode, a solution is plotted through that point. Additional clicks produce more plots; the plots are saved in graph ...
7
votes
3answers
77 views

TagSetDelayed on a function with a delayed value

I'm trying to automatically optimize cases when the user evaluates Length[returnsList[x_]] to instead use returnsLength: ...
4
votes
2answers
81 views

Defining arbitary Derivatives of functions

If I have two functions like these f[a_,b_] and g[a_,b_]. Lets say ...