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

1
vote
1answer
191 views

Equating and solving equations that have been generated using table[]

I am in the process of trying to use Mathematica to find the coefficients of transmission and reflection for the wavefunction of a beam of particles tunnelling across any number of identical potential ...
0
votes
2answers
117 views

Unwrapping a list when invoking a function [duplicate]

The minimal working example of my problem is as follows: l = {1, 2, 3, 4} f[a_, b_, c_, d_] = a + b + c + d Now, I'd like to evaluate ...
0
votes
1answer
245 views

Function to marginalize a table over given parameters

Let us say I have generated a table tab with entries {p1,..,pd,chi2[p1,..,pd]} of dimension ...
0
votes
1answer
56 views

Extracting function variables from a list [duplicate]

Evaluating the following func[x_, y_] := x^2 + y^2 listn = {{a, b}, {c, d}, {e, f}}; Map[func[##] &, listn, {1}] yields {func[{a, b}], func[{c, d}], ...
0
votes
1answer
84 views

Unable to import functions from external file

I'm fairly new to Mathematica but I'm in the process of setting up some basic analysis based off someone else's work. They had a number of shared functions stored in a shared code directory and were ...
5
votes
4answers
152 views

Make second argument same as first when only one argument passed to function [duplicate]

Clear[f] f[n_, k_: n] :={n, k} f[x] {x,n} Actually I would like to get: {x,x} The following is one wrong definition, I'm showing that I need the ...
8
votes
1answer
125 views

Is it possible to write a function immune to symbol redefinitions in any Block surrounding its invocation?

Normally, semantics of symbols used in a function definition can be changed by their redefinitions in Blocks surrounding the function invocation: ...
0
votes
0answers
37 views

Nested For Loops that result in no output [duplicate]

I am sure that this is not the most elegant code you will have seen, but I am trying to loop through an expression to see if FultonHarrisn] produces any outputs ...
8
votes
2answers
320 views

How to create custom Graphics primitive?

How to create custom Graphics primitive? It should have the following properties, resembling properties of built-in geometric figures, like ...
8
votes
1answer
117 views

Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?

Consider the following expression: Block[{Print = CopyToClipboard}, doSomething[]] Now all calls to Print from ...
6
votes
1answer
184 views

Pattern definition for replacing plus and subtract

Suppose I have two functions $h_1(x)$ and $h_2(x)$, for which we know their sum and their difference: $$ h_1(x)+h_2(x) = d(x) \qquad h_1(x)-h_2(x) = i f(x) $$ I have a complicated expression, in ...
1
vote
0answers
73 views

How can I compile function with Apply?

For example, I tried to compile this function but Mathematica gives "The only function arguments supported are Times, Plus, or List", I have no a way to compile this. ...
0
votes
1answer
56 views

Function that displays set of integers fulfilling constrictions

I would like to make a function of three parameters $i$, $j$ and $k$ that displays a list of integers fulfilling some criteria. Specifically the set of integers is: $\zeta_{ijk} = ...
7
votes
1answer
145 views

FullDefinition and Module local variable

I have a question about FullDefinition and how it interacts with the local variables of Module. I've created a few functions ...
14
votes
7answers
564 views

A function that accepts a pair or a list of pairs

Probably a duplicate, but it's not easy to search on "lists". I have a function that accepts a list of lists, say {{a1, b1}, {a2, b2}, {a3, b3}} and performs some ...
1
vote
1answer
86 views

Cannot Plot Output of Solve

I'm trying to plot the output of the following function t2[x_] in ...
2
votes
0answers
112 views

correct usage of ParallelTable

I am trying to parallelize my script. Here is a MWE of how I have done it: ...
6
votes
0answers
105 views

Is there a function to generate a minimal clique cover?

This is a graph problem known as the "Clique Edge Cover" or "Intersection Number" problem, and the goal is to find, from a given graph like $E=\{\{a,b\},\{a,c\},\{b,c\},\{b,d\},\{c,d\}\}$ and ...
7
votes
2answers
128 views

Change some option of a function when calling it [duplicate]

Say I have defined some function like f[p_] := Plot[Sin[p*x], {x, 0, 2 Pi}] Now in most cases, I just need to specify the value of ...
-1
votes
1answer
190 views

How to define a pure function with a Module?

I was not able to use a self defined (using Module and For loops) piecewise function to fit data. It seems that the problem is that I have to define a pure function. I would like to learn how to ...
1
vote
0answers
45 views

Using anonymous functions instead of module or block. Bad idea? [duplicate]

Here is an example function which returns True if a point p is on an (open) line segment (A, B) assuming that Orientation[A, p, B] returns 0 if the three points are collinear: ...
3
votes
2answers
92 views

How to apply multiple/complicated requirements for a pattern in a function input [duplicate]

In this question. I suggested that to test whether the input of a function was a matrix of numbers one could create the following test: ...
0
votes
1answer
54 views

Have the kernel load a user-defined function by default [duplicate]

I wrote a very simple function that I would like to act as if it were one of the default functions that came with Mathematica, so that you don't have to load or import any package before you make use ...
0
votes
0answers
53 views

Question regarding function definition overloading / argument passing test [duplicate]

It is possible to define function dependently to passed argument type or condition this argument fulfills (somewhat like overloading): ...
21
votes
4answers
618 views

Why is there no PositionFunction in Mathematica?

Too often I have seen the programs of inexperienced users greatly slowed by using Position in an iterative fashion, when far faster would have been to compute a ...
0
votes
0answers
23 views

How to assign values to a function at discrete points? [duplicate]

Suppose we are interested in a function f[x] that is defined only at discrete points: x = {1.1, 2.1, 3.1, 4.1, 5.1} We want to ...
5
votes
3answers
196 views

Applying a function with the HoldAll attribute inside NestList

I'm trying to write an update function, which can be applied to a list and then to NestList it. As the function has to manipulate the given variable I figured I ...
2
votes
2answers
280 views

Write a function that returns the logarithmic derivative

How can we write a function that if we input an expression f, it returns the log derivative $\frac{1}{f} \frac{df}{dx}$. We have to use a conditional or pattern test so that the function accepts any ...
29
votes
2answers
777 views

Variable naming changes everything

I am having a rather unusual problem I do not understand with Mathematica where renaming one of the variables of my function causes the function to stop "working". Here is the example of the code ...
4
votes
2answers
572 views

How to define a polynomial/function from an array of coefficients?

I have the coefficients of my desired polynomial in an array CoefArr (I'm new to mathematica, so I think of everything as arrays, it is actually a list I believe) starting with the constant at index ...
2
votes
2answers
97 views

Putting a matrix inside function

I want to fit a function to some data and inside the function there's a list here's my data ...
-1
votes
2answers
304 views

How can I define a function which is the sum of other functions?

I have a function which is summation of another function over different i,j iterators. U[r] = Sum[V[r, i, j], {i, 1, 5}, {j, 6, 10}] and ...
4
votes
3answers
162 views

How can I combine two Pick expressions into one?

I want to speed up my code, i.e., replace Select by Pick. I think using two Pick ...
1
vote
1answer
575 views
2
votes
3answers
135 views

Map several functions in one routine

I have several functions, let's assume they are: func1[x_]=x; func2[x_]=3*x-5; func3[x_]=0.1*x^2; and a lot more like these. For each and every one of these I ...
5
votes
4answers
366 views

Any rule of thumb for converting a simple mathematical expression into a pure function?

By simple mathematical expression I mean one with a few functions and few variables copied into MMA in a mechanical way from a math book. For example this one, giving the distance between two points ...
7
votes
4answers
421 views

How can I shorten this code to rotate a line segment around its center?

I have a list of line segments stored in the form: { {{x11,y11},{x12,y12}} , {{x21,y21},{x22,y22}} , ... , {{xn1,yn1},{xn2,yn2}} } Now I want to rotate all of ...
0
votes
2answers
502 views

Why function cannot be defined inside For loop? [closed]

I have a following code (which is simplified version of what I am doing): For[i = 1, i <= 5, i++, f[x_] := Sin[x]^2 Print[{i, f[i]}] ] And the question ...
2
votes
2answers
94 views

Rising Recursion Relationships

Lets say I want to compute the following function in mathematica: $G[n,k]=G[n+1,k-1] + G[n+2,k-2]$ where I know that $G[n,0]=n$ and $G[n,1]=n^2$. So, for example, $G[3,2]=G[4,1]+G[5,0]=4^2+5$ or, ...
0
votes
1answer
88 views

Function giving negative value when it should be positive [closed]

I am trying to evaluate $$\displaystyle J=A\sum_{k=0}^{n-2}(-1)^k{n-2 \choose k}B^{n-2-k}\left(\frac{C_1}{k+n}-\frac{C_2}{k+n+1}+\frac{C_3}{k+n+2}-\frac{2}{k+n+3}\right)$$ in Mathematica as a function ...
0
votes
2answers
381 views

Fast Autocorrelation Computation

I want to compute the Autocorrelation Function (ACF) of a data table with $10^6$ entries. I know that there is a built-in function in Mathematica for that, but because I do not know how exactly it is ...
11
votes
3answers
254 views

Pass by reference for an option argument

Pass by reference can be faked by using HoldAll or something similar in the definition of a "Function". But can pass by reference be faked for symbols passed as ...
5
votes
1answer
476 views

Stereographic Projection

Say I want to represent points of the complex plane in the sphere $\Bbb S^2$ using stereographic projection. That is, the Riemann sphere: Specifically, it would be nice to be able to: Given the ...
19
votes
1answer
359 views

How can I make threading more flexible?

Threading automatically with Listable functions requires the argument expressions to have the same length (or for one of them to be atomic). For nested lists the ...
9
votes
3answers
276 views

How to program a F::argx message?

How useful is it to program a user-built function in a package to produce a red warning message F::argx if you give the wrong number of arguments to that function? ...
1
vote
1answer
84 views

NMaxmize with a list of variables

I want to use NMaximize to find the maximum value of a function with multiple variables. The problem is I would like to set this up so the number of variables is only decided when the function sees ...
4
votes
2answers
84 views

Creating functions from output of other calculations

Apologies in advance if the title is vague, I'm not really sure what to call this. I have a function (call it 'foo') that generates a largeish polynomial, and it is natural to make the variables be ...
1
vote
1answer
126 views

Finding coefficients that make certain functional expression vanishing

My problem is to find a procedure that gives coefficients which make certain functional expression vanishing. For example if I have a polynomial $P(x)$ of one variable $x$ given by $P(x)=a_0+a_1 ...
32
votes
9answers
7k 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 ...