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

8
votes
1answer
136 views

How to construct the RHS of Wick's theorem

I've been trying this for a few days now, but I just can't seem to find an efficient (speed-wise) functional way to code the following task. Context: I would like to construct an expressions that is ...
4
votes
1answer
27 views

How is Attributes protected and yet can still be set?

The Attributes function itself has the attributes: Attributes[Attributes] ...
0
votes
0answers
25 views

Is there a way to save the output from integrate as a function? [on hold]

Is there a way I can save the result of my integration as a function? I would like to be able to assign the result of this integration to a function which accepts ...
8
votes
6answers
458 views

How to generate this structure of a list without `While` and the like?

I need to create a list that holds, for given integer d, the elements $1,\ldots,d,d+2,\ldots,2d,2d+3,\ldots,3d,3d+4,\ldots,d^2$ where $\ldots$ is just denoting ...
4
votes
3answers
152 views

Number by permutation

I want a function that compute number of permutation in lexicographical order. E.g. ...
2
votes
1answer
161 views

Passing function as argument

I've been mucking around with passing functions as arguments to other functions and via trial and error seem to have something that works. I was wondering if someone could have a quick look at some ...
1
vote
1answer
35 views

Plot is empty (probably) due to poor function definition

I am trying to plot my response $x(t)$ as a function of system parameter $M_0$. Here is my code to regenerate my problem ...
0
votes
0answers
38 views

How do I construct and list an operation on numbers as a function? [closed]

To write a program that list all of numbers from 1 to n that are not multiples of 4, and that doesn't use print command, I tried to input the following code ...
1
vote
2answers
87 views

n fold convolution of a function via a recursion function

I wanted to write a recursion function to calculate the n-th convolution of a function f[x_]. I wrote the function ...
6
votes
2answers
158 views

Sets and sums of different elements

Consider the set S={w,x,y,z}. If the set of all possible sums of any three different elements from S is {-1,3,5,8}, then what is the set S? How could I solve this function using Mathematica?
7
votes
2answers
94 views

For loop works at top-level but not in a function

Why does this work: For[i = 1, i <= Dimensions[list][[1]], i++, list = Insert[list, i, {i, 3}]] But this doesn't: ...
1
vote
1answer
67 views

Remove part within square brackets for function

So I've defined a variable as follows: q = {{x[t]}, {Φ[t]}} V=0.5*x[t]^2*m^2+0.5*Φ[t]^2*I I've had to differentiate with respect to time so: ...
4
votes
1answer
70 views

How to obtain the Derivative of a pure Function of variable number of arguments?

Here is a function f that takes a variable number of arguments: f = Times[##]*Log[1 - Times[##]] & I now want to ...
0
votes
1answer
89 views

How to turn a subroutine into a function?

I've written a subroutine that takes in 6 matrices and after some manipulation returns back 3 new ones. I will use this subroutine fairly often in this notebook so I think it would be easier to turn ...
4
votes
2answers
99 views

How do I localize an assignment to expression (instead of symbol) within scoping construct?

Is it possible to localize an assignment to an expression (defined via UpSet) instead of a Symbol within a scoping construct? ...
0
votes
0answers
44 views

How to calculate a continuous function once and access values later?

I have a function that involves a numerical calculation: h[v_]:=NIntegrate[f[x]*Log[p[x, v]], {x, -Infinity, Infinity}] I need to visualize how well a bunch of ...
1
vote
1answer
51 views

AbsoluteTiming of nested integration gives strange result

I found somewhat strange behaviour of nested integration timings. Consider the following code: ...
1
vote
1answer
92 views

Plotting a periodic function [duplicate]

My question is the following: how can I plot a function repeatedly? By following some topics here I found some basic information about using Fourier series. This is what I made: ...
0
votes
1answer
37 views

Why doesn't this function definition work?

I am trying to define a following function (which is a convolution integral) ...
2
votes
2answers
110 views

How to set a values inside a custom function [duplicate]

I have a custom function like this stringLength[string_] := StringLength[ ToExpression[StringJoin[ToString /@ {del, string}]] = StringDelete[string, " "]] ...
1
vote
2answers
55 views

Finding roots of a function that includes Bessel functions [duplicate]

I'm fairly new to Mathematica so forgive any stupid mistakes. Here's my function: ...
0
votes
3answers
71 views

Write complex number in terms of absolute value and phase

I have a long vector and some of the values (19 out of 64) are complex. I got them using the Mathematica Rationalize function, so the complex ones are written in the a+bi form. Is there a function I ...
0
votes
0answers
58 views

Considering special cases in general definitions

I want to calculate some coefficients $\qquad B_k = 2\int_0^1 u(x) \cos(k\pi x)\,dx$ for $0<k\in\mathbb N$ and I am doing this in the following way: ...
0
votes
2answers
109 views

How do I get Series[] as a functional, rather than as an expression (i.e. to avoid the dummy variable)?

How can I write an equivalent to Series that doesn't require a dummy variable? Note that the series should be constructed before the evaluation point is supplied, ...
17
votes
3answers
361 views

Writing functions with “Method” options

I'd like to implement several behaviors for a particular function using a Method option added to the function definition: ...
0
votes
0answers
52 views

break down the function

I am using Series function, but I found that it cost too much time. Since the Series function is Taylor expansion and I only need first order, I want to break down the code, so I can make it faster ...
1
vote
2answers
75 views

Partial Function Evaluation [closed]

How can I evaluate only part of a stack of function calls? In particular, given f[x_,y_,z_]=x+y+z;, say, I'd like to define a function ...
2
votes
1answer
47 views

Passing a list to a nested vector function

In order to apply a newton-rhapson algorithm to solve a set of nonlinear vector equations such as these, f[x_, y_] = {x^2 - 3*y + 6, (y + 1)^2 - x - 33} I need ...
4
votes
4answers
84 views

Multi-index variable depending on signature of permutation of indices

I do have some multi-indexed variable, e.g. like this \begin{align*}f_{123} &= 1\\f_{345} &= 1/2\end{align*} where $f$ is antisymmetric under permutation of any pair of indices, i.e. e.g. ...
1
vote
1answer
37 views

How to use Composition when the functions have two parameter

There are some basic instructions about Composition on this site, but I'm still confused on how to use it when the functions I am composing have two parameter, such ...
3
votes
2answers
245 views

Why can't FindFit get the proper result in this problem?

Cross-posted in http://tieba.baidu.com/p/4425634583 The mathematica helps me solve one differential equation in numerial method and got the data of the answer,but the taylor series fit is not good in ...
3
votes
4answers
3k 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
58 views

How to use currying to write a tail recursive function for summing factorials given a certain bound

I am very confused about how can i use currying to write a tail recursive function for summing factorials within a certain bound or summing the bounds i.e. given [1,5] i should be getting ...
3
votes
1answer
66 views

Mod[k, 2] == 0 versus EvenQ[k] [duplicate]

I'm having some trouble with Mod[k, 2] == 0 versus EvenQ[k] They are sometimes yielding different results when given the ...
0
votes
1answer
45 views

ND on a function using NIntegrate

I need to numerically differentiate a function that takes a list as an argument and that uses NIntegrate in its definition. A simple example is ...
3
votes
1answer
88 views

Writing a function that finds the difference between two maxima

Suppose i have two functions: $\qquad -2x^2 +5x+3$ and $\qquad -5x^2 +2x+3$ To solve the point where the functions are at maximum i use the command NMaximize ...
3
votes
1answer
109 views

Jackson Integral

Following an old post, I have tried to make a function that calculates the Jackson integral of $q$-calculus: ...
5
votes
1answer
217 views

How to Override the Head of a Symbol

I was wondering if it is possible to override the head of symbols/operators. As a motivating example if I run: Then Ket will be returned. But lets say I want Bob to be returned instead of Ket. Is ...
0
votes
1answer
39 views

Finding text files in a given path and applying a function to those files

I made a function called "SolveLinearSystem[]" that solves,as its name suggests, a linear system of any amount of variables. It takes the path of a text file as its only argument. The function then ...
0
votes
0answers
50 views

Normal ordering in Mathematica

I want to create a function which normally orders a string of field operators. Consider the following: $$\langle0\vert\hat{a}(k_n) \cdots ...
0
votes
0answers
52 views

Problems with Tooltip

I an getting strange behavior with functions built using Tooltip. I create a function to display the position of data in a list ...
3
votes
2answers
121 views

Computing powers of the operator using symbolic computation

Suppose $t\in\mathbb{R_+}$ - some parameter, $V: \mathbb{R}\to\mathbb{R}$ - some function. I have an operator $S:f\mapsto S[f]$ that maps a function $f$ to a function $S[f]$: $$ S[f](x) = ...
4
votes
3answers
193 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
67 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
39 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 ...
4
votes
3answers
89 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: ...