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

0
votes
0answers
75 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
91 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. ...
3
votes
2answers
47 views

How to add Optional Sequence

Note: This question could be duplicate but I couldn't find any so far. It is know that Optional (:) can be use to represent expression if omitted. ...
0
votes
1answer
50 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
63 views

Should I define a function or not?

I was trying to execute the following code: ...
-1
votes
2answers
91 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 ...
8
votes
1answer
186 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
294 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
1answer
86 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
132 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
78 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
102 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
35 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
95 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
207 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
126 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
100 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 ...
10
votes
2answers
930 views

Number of digits for factorial of 12345678987654321

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 086 419 746 913 569 ...
5
votes
1answer
125 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
863 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 ...
7
votes
3answers
90 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 ...
5
votes
1answer
58 views

Multi-case function: many single-case delayed assignments; or one Which[]?

A function needs to cope with many different forms of input, such as different heads (Plus, Times, ...
12
votes
4answers
206 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
125 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
98 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
88 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
43 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
150 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
304 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) ...
19
votes
1answer
258 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, ...
0
votes
1answer
30 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
46 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: ...
6
votes
2answers
290 views

More elegant function construction for blues loop

Is there a more elegant way to do this: ...
3
votes
1answer
91 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
86 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
95 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
82 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
89 views

Defining arbitary Derivatives of functions

If I have two functions like these f[a_,b_] and g[a_,b_]. Lets say ...
1
vote
1answer
95 views

Using a table as a variable in a function

I have a bunch of arrays and need to extract specific rows and columns of these that obey certain criteria. So for one table I'd do like this ...
1
vote
1answer
39 views

Please explain this pattern/Message related issue and help with finding workaround

I attempted to clean up my code and also add messages to functions if they are called with wrong arguments. This introduced some error which I tracked down to the following: ...
2
votes
2answers
88 views

Function where variable is integration bound

Lets say I have a function of the form: $f(T) = T^4 \int_0^{1/T} \frac{x^3 dx}{e^x-1}$ This is an example of the situation I am describing not a unique problem. I want to define it as a numerical ...
2
votes
1answer
46 views

combine two definitions into one [closed]

Suppose I have two definitions of a function such as ...
5
votes
3answers
212 views

Make own rule for multiplication

I'm new to mathematica, so I still cannot use it properly. I want to do symbolic programming. My question is : is there any way to define our own multiplication. Suppose $a,b$ are arbitrary ...
1
vote
1answer
82 views

Define recursive function over two indices

I have $x_i=(i-1)/50$ for $i=1,\ldots,51 $. Given a function $ f(x) = x^2 $, I want to find $y_i^k$ given by \begin{eqnarray} y_i^1 &=& f(x_i) \\ y_i^{k+1} &=& ...
6
votes
3answers
316 views

How can I create currying functions using pure function syntax?

For example how can I write In: pureFunctionSyntax[myF] /@ {7, 3} Out: {myF[7, Log[7]], myF[3, Log[3]]}
3
votes
1answer
84 views

InverseFunction: how can I extract the function inside?

This is probably a trivial question. But I could not find an answer. Given the explicit expression for ft in te assignment ...
1
vote
0answers
50 views

Mapping over two indices with a condition

How can I use Map over two indices with a condition? I am trying to calculate second derivative of an eigenvalue, $\lambda_i(x)$, of $n \times n$ matrix $M(x)$ ...
2
votes
1answer
99 views

Why must I specify the Global context for a symbol in a private package?

I have written two versions of the function below in a package. When I load the package and call the function, Why does the first version where the variable s has no explicit Context return the long ...
1
vote
0answers
46 views

Piecewise in 2D with quadrants as conditions

I am trying to define a piecewise vector field. Can the condition be of the form I am using (i.e. a quadrant)? I can't also make Piecewise work with more than two ...