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
275 views

Can Mathematica functions have static variables?

In C, one can have variables that are declared static. These are not re-initialized every single time a function is called. ...
1
vote
1answer
86 views

Possibility of evaluating the expression within a NestList at each iteration?

I want to rotate iteratively a triangle about the same point, increasing for each iteration the length of its side in the same proportion and change the color of its sides. I came up with the solution ...
3
votes
1answer
105 views

Graphics Function Definition Style (Using SubValues vs. DownValues)

I am going through some of the items in "Modern Differential Geometry of Curves and Surfaces with Mathematica, Third Edition". An example of the style used by these authors is: ...
19
votes
3answers
365 views

Why x = x doesn't cause an infinite loop, but f[x_] := f[x] does?

If I execute: In[1] := x = x Out[1] = x and then I evaluate the symbol x: ...
1
vote
5answers
94 views

Question about sum and list in a function

I have a list of this form: $ lst =\left\{\left\{q_1,x_1,y_1,z_1\right\},\left\{q_2,x_2,y_2,z_2\right\},\text{...},\left\{q_n,x_n,y_n,z_n\right\}\right\} $ I want to use this list in the function: ...
3
votes
1answer
153 views

What does it mean when a function is defined with two pairs of brackets

I have noticed a function defined as such: f[x_][y_] := expr; How is the argument y in 2nd pair of brackets being used? What is the difference from defining a ...
2
votes
3answers
137 views

How to generate a series of bisection points of a given line segment?

I want to get the coordinates of the bisection points for a given line segment to a specified resolution. For example, when the line is given by {0,0} and {1,0}, the output coordinates should be ...
0
votes
1answer
112 views

A function generating particle-in-a-box eigenfunctions

The function sequence is defined over $[-\frac{L}{2},\frac{L}{2}]$ (all elements are valued 0 outside this interval): $$\psi_n(x)=\begin{cases}\sqrt{2/L}\sin{(\frac{n\pi x}{L})} & n \space even ...
1
vote
3answers
172 views

professionalize/optimize my code that calls a function

I have the following construction, which defines a function that I subsequently call in a loop multiple times. I use this very often, but I have never looked into if there is a more professional way ...
1
vote
1answer
114 views

Module that finds the sum of digits of any given positive integer

How can I make a module that finds the sum of digits of any given positive integer? I use this command F[x_]:= Apply[ Plus, IntegerDigits[x] ] Is there another ...
1
vote
1answer
164 views

Part::pspec error returned when using a ListLinePlot ColorFunction variable as list index

In ListLinePlot, I'm attempting to construct my own ColorFunction for coloring points differently based on a third dimension of data not plotted to a specific axis. However, ColorFunction only uses ...
2
votes
2answers
80 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 ...
4
votes
2answers
174 views

Is it possible to `Protect` a dynamic programming process?

I wrote a function using dynamic programming to speed up the evaluation. For safety, I also want to protect this function. Take factorial for example: ...
2
votes
1answer
79 views

Need to run evaluation 2 times

Why does this only work after running the evaluation 2 times? ...
8
votes
2answers
231 views

Automatically compute r[1] = r1; r[2] = r2; …

I have three variables like this, r1 = 4; r2 = 3; r3 = 1; and I want to insert them into a function use them in a function definition like so: ...
1
vote
1answer
94 views

Linearity of a function in Mathematica

I have a function which has something like myFunc[q,a state[c,d]] a could be anything, and I want to tell Mathematica that ...
5
votes
2answers
225 views

Manipulation of InterpolatingFunction

Suppose that $q[t]$ is obtained by NDSolve as an InterpolatingFunction, and I want to define $Q[t]$ to be some function of ...
3
votes
3answers
206 views

Prime factor counting function

Is there any way I can speed up this prime factor counting function? (I am looking for all numbers in a range with 3 prime factors (counted with multiplicity).) ...
1
vote
3answers
168 views

Pure function inside another pure function

I have the following working expression: In[15]:= Length[Select[IntegerPartitions[10],First[#1]==5&]] Out[15]= 7 But, instead of using the constant 5 I want ...
4
votes
3answers
709 views

Make a differentiable smooth sawtooth waveform

So, I am trying to understand the effect of sawtooth waveform on a mechanical system model. For example, a system of a plate and a box on it. And then we try to oscillate the plate. In order to do ...
8
votes
3answers
192 views
3
votes
0answers
56 views

How to combine DifferenceRoot objects for odd and even-indexed terms

I'm trying to analyze a certain infinite sequence $S$, indexed by positive integers starting from $1$. It can be split into two subsequences: $S^{odd}$ by removing all even-indexed elements and ...
1
vote
2answers
167 views
0
votes
2answers
140 views

How to insert trigonometric functions into matrices' vector components? [closed]

Is it possible to insert a trigonometric function into a matrix which will be able to be used dynamically, as an element of one of the vectors of the matrix? I'm trying to perform an operation like ...
-4
votes
1answer
110 views

Programming Multipolar Expansions in Spherical Tensors

We have that a density matrix can be written in a basis of spherical tensors: $\rho =\sum _{K=0}^S\sum_{q=-K}^K \rho _{\text{Kq}}^{(S)} T_{\text{Kq}}^{(S)}$ for example for a 3x3 matrix, $S=1$, ...
0
votes
1answer
97 views

Is it possible to define a function in terms of a power series?

Is it at all possible to define a function in Mathematica in terms of a power series? For a specific example, I wish to write $x(\tau)$ as $x(\tau)=x_{0}(\tau)+\mu ...
1
vote
1answer
111 views

Orthogonalizing polynomials with inner product depending on parameters

I need to orthogonalize the polynomials $h_n(x)=x^{2n}(1+x^2)^{-4S}$ with $x\in\textbf{R}$, $2S\in\textbf{N}$ and $n\in\{1,3,5,\ldots, 4S-1\}$ over the inner product $\langle h_n,h_m\rangle=32\pi ...
5
votes
1answer
65 views

How to bail out from a non-applicable transformation function?

I provide a custom function in TransformationFunctions options of FullSimplify that can do a transformation only for some values ...
3
votes
2answers
310 views

Algorithm for parts integration

Sorry if this is a duplicate, I've searched how to do this to no avail. What I'd like to do is a function that integrates by parts $n$ times, i.e $$ \int u(x) v(x) dx = u ...
0
votes
2answers
102 views

A function that (also) changes a replacement rule

I'd like to write a function operating on a list, returning a list, but also changing a replacement rule. Let's say I have a list of replacement rules: ...
3
votes
1answer
125 views

Define a function with the condition that it must be positive

I have a function f(x) and I want to define a new function g(x) that is equal to f(x) only ...
18
votes
1answer
285 views

Kernel inexplicably hangs if I define a pure function without suppressing output

Save any unsaved work before trying as it may crash Mathematica and other programs by filling up the memory! In v9.0.1.0 on OSX 10.8.5, if I start a new session and define two functions ...
12
votes
4answers
365 views

Equivalent Nested Loop Structure

Consider the following examples: ...
1
vote
1answer
198 views

Gram Schmidt inner and outer products

I know the Gram-Schmidt orthogonalization generates an orthonormal basis from an arbitrary basis. I need help with: Write a program that inputs a list $\{b_1,\dotsc,b_n\}$ of linearly independent ...
6
votes
2answers
186 views

Composition of functions using slots

Apolgies if this has been asked and answered, but I'm not even sure what to search for. I have two functions defined as follows: ...
6
votes
2answers
209 views

How to implement a formal expectation operator over an unknown distribution?

I need to do some simplification of an expression involving averages over a stochastic variable (in order to verify a long analytical calculation). The easiest way to do that, I figured, were if I ...
0
votes
1answer
198 views

How to define a function with different types of arguments by setting conditions on function's domain?

I need help in order to properly define a function of $4$ variables. Given that: ...
4
votes
1answer
158 views

Implementing Bilinear Products

Consider a real, vector space $V$ with basis $B=\{v_1,v_2,\dots\}$, and let $\star:V\times V\to\mathbb R$ be a bilinear product on $V$. I would like to implement this product in Mathematica by ...
0
votes
0answers
74 views

creating some smooth density functions

I would like to create some density functions based on some distance measure $D$. As a base density function lets say I have a Gaussian density $f$ with mean $-1$ and variance $1$. I would like to ...
1
vote
1answer
146 views

Writing NDSolve-like functions

I've been using Mathematica for a while now, and there are multiple common tasks that I end up doing. I'd like to learn how to convert them into functions so that I can store them in some file and ...
0
votes
2answers
80 views

I want to define a function that has integer power [closed]

I am defining this function: f[x_] = kr*g[x] + (c^l*x^l)/(KIR + c^l*x^l) But I want to make l bigger than 0, so if I evaluate ...
1
vote
1answer
122 views

Defining commutation relations with Flat functions

Here is a minimal example of the problem I am having. ...
6
votes
3answers
163 views

Using PatternTest and/or Condition to detect equal list lengths as well as list element formats

On searching Mathematica.SE for information about overloading functions, I came across several threads about PatternTest and ...
1
vote
0answers
40 views

Get pattern from rising numbers [duplicate]

I am no mathematician or math star, yet wondering if there is a formula or a way to calculate what follows: I have series of numbers: ...
2
votes
2answers
503 views

Q-Function representation in Mathematica

I observed that there is no Q-function representation in Mathematica. The definition of Q-Function is: \begin{align} Q(x) &= \frac{1}{\sqrt{2\pi}} \int_x^\infty e^{-\frac{u^2}{2}}du \\ ...
2
votes
2answers
111 views

bulk initialization of associative array/hash implemented with function pattern matching?

I see that we can do associative arrays in Mathematica using functions that have implicit pattern arguments, as in ...
1
vote
3answers
110 views

Declare local variable when defining a function to mimic Table[]

The problem: I want to define a function tab to replace the long line Table[f[i,j,k,l],{i,3},{j,3},{k,3},{l,3}] by a shorter ...
2
votes
1answer
85 views

Is it possible to use a mathematical expression as function name?

This might seem as a silly question, but is it feasible to use a mathematical expression as the name of a function? For example, I'm studying the cross section of Compton scattering and there is a ...
19
votes
2answers
264 views

Plotting a defined function gives different results from plotting the function directly

Consider the following piece of code: function[t_]:=Exp[-9 t^2]; LogLogPlot[{function[t],Exp[-9 t^2]},{t,.01,100}] I expect the two plots to exactly overlap each ...
14
votes
1answer
310 views

How to represent a continuous monotonic phase of Airy functions?

Note: In this question I am concerned only with real-valued variables and functions. DLMF, §9.8 Airy Functions, Modulus and Phase, formula $9.8.4$ defines the phase of Airy functions: ...