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
1answer
42 views

Why doesn't this function definition work?

I am trying to define a following function (which is a convolution integral) ...
2
votes
2answers
116 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
79 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
89 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
59 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: ...
19
votes
3answers
394 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
56 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
89 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
49 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 ...
5
votes
4answers
96 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
41 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
256 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: $$\Big[\frac{1}{\sin\theta}\frac{d}{d\theta}\big(\sin\theta\frac{d}{d\theta}\big)-\frac{m^2}{\sin^2\theta}+n(n+...
4
votes
1answer
68 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 1!+2!+3!+4!+...
3
votes
1answer
71 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
47 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 ...
4
votes
1answer
91 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 ...
4
votes
1answer
127 views

Jackson Integral

Following an old post, I have tried to make a function that calculates the Jackson integral of $q$-calculus: ...
4
votes
1answer
220 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
42 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
60 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 \hat{a}(k_2)\hat{a}(k_1)\hat{a}^\dagger(k_1)\hat{a}^\dagger(...
0
votes
0answers
55 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
123 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) = f(x+\sqrt{t})...
4
votes
3answers
207 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
70 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
129 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 ...
4
votes
2answers
181 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
313 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
187 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
30 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
48 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
30 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
246 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}\...
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
53 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
50 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
261 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
243 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: ...