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

4
votes
3answers
202 views

Recursive definition of nested functions

This is probably very easy, but I cannot figure out a way to define a function like: $$ g_h = ( 1 + f_1(1+f_2(1+f_3(.. (1 + f_h)))))$$
1
vote
1answer
69 views
1
vote
0answers
52 views

A kind of function optimization problem [duplicate]

I have the following problem. Out of a calculation I got the four functions shown at the end of the post. These functions depends on many variables, qk, pk etc (all of them are actually scalar ...
1
vote
1answer
40 views

How do I convert an expression that is a function of some variables into a functional?

If I have an arbitrary f[x, y, z], how do I convert it to f[#1, #2, #3] & without messing up scopes or causing scope leakage?...
4
votes
3answers
92 views

Set absolute options for a function?

How can I set an absolute option for a user-defined function? I understand I can define options like this: ...
5
votes
3answers
128 views

How to define a function whose parameter have a list with default-value

As we know,we can define a function with default-value like this: fun1[x_, n_: 1, a_: 2, b_: 3] := x + n + a + b In[6]:= fun1[4] Out[6]= 10 But in my ...
12
votes
5answers
1k 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 ...
2
votes
2answers
73 views

Finding zero of nonlinear simultaneous equations [closed]

I'm trying to find the zero of nonlinear simultaneous equations defined in myfun1 and myfun2: ...
4
votes
2answers
180 views

How can I define the majority function?

I came across the majority function while falling into a Wikipedia link rabbit hole (I wish I could remember how I got there...) The function is defined for $n$ arguments and given by $$\mathrm{maj}(...
13
votes
2answers
308 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) ...
7
votes
2answers
186 views

How to track Part of Symbol or How to symbolize Parts of Symbol without copying data

Let's create simple function: SetAttributes[h, HoldFirst]; h[data_] := Dynamic[Grid[data], TrackedSymbols :> {data}] test = RandomReal[1, {2, 2}]; h[test] ...
0
votes
0answers
25 views

Applying a function to the second element of a list [duplicate]

I'm trying to apply a simple function to the second element of a list: {#1, #2/5} &@{1, 2} But this returns an error message. ...
1
vote
1answer
47 views

Is there a way to make the Function construct accept a list as argument? [duplicate]

Is there a way to make the Function construct accept a list as argument ? Writing Function[{x,y},{x+y,x-y}] does not work, ...
0
votes
1answer
29 views

Using the Function construct in a table with parameters [duplicate]

I'd like to define a table of functions like so Table[Function[{x,y},{x + k y,x - k y}] , {k,1,10}] My intention is to have the first function defined with k=1, ...
0
votes
2answers
240 views

A function generating particle-in-a-box eigenfunctions

This function sequence is defined over $\left[-\frac{L}{2},\frac{L}{2}\right]$ (all elements are valued 0 outside this interval): $$\psi_n(x)=\begin{cases}\sqrt{\frac2{L}}\sin{\left(\frac{n\pi x}{L}\...
3
votes
0answers
103 views

Obtaining a Previously Constructed Function as Desired Output

I was playing around with some Generalized Trigonometric Functions defined as follows; ...
2
votes
2answers
102 views

`PrimeNu` counting function

Building on this question, what is the most efficient counting function for distinct prime factors? It would obviously be more efficient if Prime and ...
25
votes
2answers
1k views

Is there any way to define pure functions with optional arguments?

For example consider the following function: Function[{u,v},u^2+v^4] Is there anyway to define default values for the variables ...
1
vote
2answers
55 views

Importing data, then plotting ListPlot. What's the most efficient way to write a code?

It should be a boring question to ask for someone, but this is very basic need which I haven't resolved by myself. I know I can use MapThread and pure function to make it short and beautiful, but even ...
1
vote
1answer
52 views

Using the Nest function — Confusion on my output

I have the following code and output where the original function is $f(x,a) = a^2 - x^2$. ...
0
votes
1answer
44 views

Convert string to function with an argument

I am trying to build an expression by first writing it out as a string and then using ToExpression to convert it to an expression. However, I cannot figure out how to make expression recognize x as an ...
4
votes
1answer
137 views

Are these two functions equivalent?

I checked several cases and saw that two functions are equivalent in these samples. However, are the two equivalent in all cases? Is there a way to check that? Explicit function: ...
2
votes
0answers
50 views

First position of incremental series

I am still a beginner and maybe I want to do something too complicate! After 29 cycles I obtained the following events list: ...
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, ...
19
votes
1answer
260 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, ...
8
votes
3answers
241 views

How to create a version of FixedPointList for asymptotically periodic functions?

I need to efficiently generate a bunch of bifurcation diagrams. A simple familiar example illustrates the efficiency problems I'm running into: ...
1
vote
2answers
76 views

Problems using a function as an argument

I made a function: f[x_]:=x^2+1 and when i use it on, for example f[5], i get the answer 26. . But when i use it on: PollardRho[a_, f_] := Module[ {}, Return[f[a]] ] And when i write ...
3
votes
2answers
49 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. ...
15
votes
3answers
1k 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 ...
10
votes
2answers
948 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 ...
1
vote
0answers
49 views

NonLinearModelFit to a Function [closed]

I have a multiple dimension function that is fairly complicated. Instead of simply using Series to get a taylor series of the function, I would rather specify my ...
1
vote
0answers
78 views

Pattern for a function of say 2 variables [closed]

How can I specify a pattern for an expression f for which say f[x,y] returns a value, ie is defined as a function taking two ...
3
votes
2answers
256 views

Handling options in my function [duplicate]

It seems this should be easy, by I just can not figure out how. I'm making many very similar style plots, so I want to make a function with all the default options specified. Sometimes I need to tweak ...
1
vote
1answer
160 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. ...
43
votes
10answers
9k 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
44 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 ...
1
vote
1answer
85 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 ...
8
votes
6answers
276 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 ...
6
votes
2answers
141 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
140 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
82 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
144 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
50 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
80 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
50 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. ...
3
votes
1answer
121 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
98 views
0
votes
1answer
48 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.: ...