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
2answers
1k views

Gram Schmidt Process for Polynomials

I want to implement the Gram Schimdt procedure to the vector space of polynomials of degree up to 5, i.e. I want to find an orthogonal basis from the set of vectors $v=(1,x,x^2,x^3,x^4,x^5)$. The ...
0
votes
1answer
81 views

Given a sequence of expressions, how can I convert them all into functions?

I have a sequence of expressions 1, x, x^2, ..., x^5 I want to define a sequence of functions out of it, what should I do? Namely I want to define a vector of ...
9
votes
4answers
244 views

How to pass a list of arguments into HoldAll

I have a list of arguments (which in reality is lengthy): arguments = {a, b, c} arguments2 = {a_, b_, c_} f[Sequence@@arguments2] := a + b + c Note: It seems ...
9
votes
2answers
235 views

How can I get the right hand side of a delayed expression?

Imagine there is a given function f, defined with SetDelayed, say f[x_] := Sin[x]^2 + Cos[x]^2. Is it possible to get rhs of ...
0
votes
1answer
614 views

How can I define a Step-Wise function in Mathematica (Not using Heaviside Step Function)? [closed]

I need to define a function, which has very different behaviour in different regions. There are about 13 different regions. A sample of my function is the foloowing table: I want to define it as a ...
4
votes
2answers
147 views

How to only work on sublists with non-zero (or positive) values

i have a large compositional dataset which contains non-zero and zero values. here is a sample: ...
-1
votes
1answer
109 views

Dynamically show a specific number of the fractional part's digits

What is the fastest way to cut off all n z digits of the fractional part of a real number? (For example 12390.20934230) I came up with this so far: ...
11
votes
3answers
850 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 ...
8
votes
2answers
141 views

Possible to scope an anonymous function within Module?

This is my first question. I'm fairly new to Mathematica and completely new to SE, so I apologize if this question is trivial. I'm working on a problem that requires a function to be redefined ...
2
votes
3answers
187 views

Defining a function to determine when convergence is guaranteed

I'm investigating how many iterations are needed for a particular orbit to reach a fixed point of a function. Since I have many functions to test, I want to define a function in Mathematica that will ...
3
votes
2answers
293 views

Why does the first derivative of a piecewise continuous function have discontinuities?

I have this piecewise continuous function which is also continuously differentiable over time : ...
0
votes
1answer
100 views

Strange function definition result [duplicate]

I'm relatively new to Mathematica, and I'm trying to define a function f(k) that would do the following: For any positive integer $k$, a finite sequence $a_i$ of fractions $\frac{x_i}{y_i}$ is ...
2
votes
2answers
199 views
20
votes
4answers
368 views

Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

Using global variables the following turns an "expression" into a Function: expr = 2 x; Function[x, Evaluate[expr]] Of course ...
5
votes
1answer
176 views

How to find the name of the current function

I would like to know the name of the current function from within that function. For example, consider the following code ...
1
vote
0answers
91 views

How can I obtain the function described by given set of central moments?

I want to investigate how my function P behaves with different probability functions rho as input variables. This means ...
1
vote
1answer
197 views

Function definition and delayed assignment

I need to define the following function MyWavelet[n_]["PrimalLowpass", prec_ : MachinePrecision] := Table[(-1)^(j - 1) h[[2*n - j]], {j, 0, 2*n - 1}] which ...
4
votes
4answers
278 views

Differentiating space curves

I'm trying to do some very basic differential geometry of space curves. For example, a space curve $\gamma:\mathbb R\to\mathbb R^3$ has unit tangent and normal vectors given by ...
2
votes
4answers
920 views

How to write a differential operator in Mathematica

I have a very basic question. I am trying to code the LHS of the differential equation: ...
3
votes
1answer
102 views

Using NestWhileList to determine smallest prime value in series

I have a function recursively defined as follows: $a_{n+1}-1=(a_n-1)\times lpf(a_n)$, whe $lpf(x)$ is the least prime factor of $x$. Now, given an initial value of $a_0$, I would like to find the ...
4
votes
1answer
721 views

Module with “local functions”

I am trying to use a Module having "local functions", i.e., those which I need to define only inside this module. So I tried this: ...
4
votes
3answers
149 views

Creating a nonperiodic function in mathematica

I want to create a non-periodic square wave with values of 1 and -1(not necessarily alternating). For e.g. I want to convert an arbitrary array like {1,-1,-1,1,-1,1,-1} into a function. I tried ...
1
vote
1answer
70 views

Confirming the existence of a function related to a matrix

Is it possible to get an answer to the following question in Mathematica? Let $M$ be a $n$ by $n$ matrix, is there a function $m:\mathbb{N}\times \mathbb{N}\rightarrow \mathbb{Z}$ such that ...
0
votes
1answer
173 views

How could we define a function recursively?

Let us first consider the constructon of the following simple iteration ...
3
votes
1answer
127 views

Downvalues vs. Scoping for Functions

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but ...
0
votes
1answer
532 views

How do I make function A create and return function B?

My basic need is I have to take lots of data and other information and assemble it into a function (static parameters). I then need to call the function many times for numerical results for many ...
4
votes
2answers
144 views

How to achieve Set+Part like behaviour in custom Set function?

So I've been toiling away on my DataFrame package. I've been trying to get Set to work with it, but it seems it either can't be done (ie via ...
1
vote
1answer
123 views

Creating an array eval [closed]

How can I create an one dimensional array (a vector) evaluating a function of two variables? I know how to do it with one variable: Array[f, n, {a, b}] generates a ...
15
votes
1answer
221 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 ...
3
votes
1answer
99 views

Is it safe to assign a variable and function of the same name for different things?

I'm writing out a notebook that goes through the van der Waals Equation of State for gases, and I run into a situation where I want to assign (simplified) Tc[b_]:=5b, use that to solve for b in terms ...
-1
votes
1answer
736 views

How can I make a plot of the Higgs potential?

I was wondering, how would could I make a drawing of the Higgs potential aka "mexican hat potential". I am quite new to Mathematica and don't know where to look to learn how to implement such a ...
6
votes
4answers
339 views

Function argument to default under certain condition

Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met? For example, if a function is defined as: ...
2
votes
1answer
182 views

Can my permutations function be defined using iteration?

I have two functions, tuples and perm. They are similar. ...
2
votes
0answers
77 views
3
votes
2answers
134 views

Apply UpValues before Listability

I'm trying to modify Plus but am running into trouble with it being Listable: ...
6
votes
1answer
297 views

How can I define a function that can accumulate its parameter?

For example, write a function add: add[3] return 3 add[4] return 7 add[10] return 17
10
votes
2answers
251 views

Functions that remember their values

Can someone explain what is going on with the following ... func[y_] := func[y] = (Print["Hello world !!!"];) func[1] Hello world !!! ...
1
vote
1answer
254 views

Fitting parameters of user defined function returning discrete values

So, I'm fairly new to Mathematica (apologies in advance for the noobiness), and I'm having some problems regarding fitting the parameters of a user defined function to data. I'll give a representative ...
0
votes
1answer
209 views

How to write case, in piecewise function, correctly [closed]

I want to plot graph of this piecewise function in Methematica: $f: [0, 1] \mapsto \mathbb{R} $ $f(x)= \begin{cases} \frac{1}{x - \frac{1}{3} } & \text{ if } x \in [0, \frac{1}{3}> \newline ...
6
votes
3answers
329 views

How can I rewrite this with NestList or FoldList?

The original code used For: ...
3
votes
2answers
427 views

Ways to plot interpolating functions more economically?

I am interpolating a lot of data over geographic coordinates obtained via GPS, and frequently I need to plot these interpolated functions together. For example, I may have an interpolated elevation ...
3
votes
0answers
50 views

How to influence Suggestion Bar in your package/code [duplicate]

Possible Duplicate: Is it possible to customize the Suggestions Bar? I have written a little code (like a minipackage) of different functions. In using my code, the user typically starts ...
0
votes
1answer
155 views

Creating Recursive Sequences

Quite a simple question, I reckon, however, even quite an extensive search hasn't helped me. I want to define a recursively defined sequence that starts with defined ...
4
votes
1answer
163 views

Feedback on my Subscript code

I have written a code, and I need a little feed back.. I want to make the Subscript distributive across (1) addition and (2) multiplication to mimic short-hand for ...
1
vote
3answers
298 views

More than one expression in the Table[] command?

I would like to construct a Matrix using the Table[] command. The issue is that I would like to use more than one expression for it. What I need to obtain is something like this : ...
2
votes
2answers
184 views

Hiding certain user-defined functions in front-end autocompletion

I have written an elaborate code that contains a collection of functions for the user and a bunch of auxiliary functions whose existence I would like to hide away from the user. I've written my code ...
4
votes
2answers
220 views

Pattern matching repeated structures with named subexpression components

I am trying to write a function with a pattern that tests its arguments for a particular structure. But in that pattern I'd like to name a subpattern to make it easier to exract and operate on. The ...
7
votes
7answers
568 views

Can my color function be written more simply?

The code shown below works well, but I think the pure function I'm supplying to the ColorFunction option could be more elegant. How can I simplify it? ...
3
votes
3answers
126 views

How to identify patterns in a tensor-polynomial and replace appropriate symbol?

I want to write a function tensorReplace[input] that takes a tensor polynomial in $r_i$ such as $r_i r_j r_k+r_i q_j q_k +q_i q_j q_k$ , and replaces each monomial ...
2
votes
1answer
143 views

Rookie mistake in defining a function with Modules?

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of x, y and ...