Questions tagged [function-construction]

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.

Filter by
Sorted by
Tagged with
7
votes
4answers
241 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 ...
7
votes
4answers
427 views

Recursive piecewise integral relation with piecewise base case?

How is this recursive formula $$ f_{n+1}(z) = \int_0^1 f_{n}(z-y)\,{\rm d}y $$ implemented in Mathematica? The base case is $$ f_1(z) = \begin{cases} 1 & 0\leq z\leq 1 \\ 0 & \text{...
7
votes
4answers
126 views

Problem with handling a factor of $1$ in pattern matching

I am trying to implement the creation and annihilation operators $\hat{a}$ and $\hat{a}^{\dagger}$ of quantum mechanics with the following code, but alas, there is a problem with the pattern matching ...
7
votes
3answers
939 views

Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
7
votes
2answers
717 views

Define product derivative

How do I define the $n$th product derivative of a function in Mathematica? The first product derivative $f^\ast$ of a function $f$ is $$ f^\ast(x)=\exp\left(\frac{f^\prime(x)}{f(x)}\right) $$ The $n$...
7
votes
3answers
2k views

How to define the Poisson bracket in Mathematica

If $f,g$ are functions of the independent variables $\{q_1, q_2, ..., q_N, p_1, p_2, ..., p_N\}$, then the Poisson bracket is defined as: \begin{equation*} [f, g] = \sum_{i=1}^N \left(\frac{\partial ...
7
votes
2answers
447 views

Can we make a faster Boole implementation?

I just learned about Mr. Wizard's very efficient code here. ...
7
votes
3answers
252 views

Applying a function by Key pattern, preserving Association order

Any convenient ways to apply a function to Keys that match a pattern in an Association? Method should handle: Mixture of ...
7
votes
2answers
305 views

How to specify and use nested options for a function?

I want to write a function that takes options that are nested. For example, suppose the function has one main option, "method" which can take two values, "a" and "b". Also, when method is "a", it can ...
7
votes
2answers
356 views

How to understand the process of ListCorrelate when it in two-dimensional condition?

Toady,I learn a function ListCorrelate,and I can understand process of execution his result in one-dimensional. However,the ...
7
votes
1answer
314 views

Obfuscate the code for a function [duplicate]

I have written a Mathematica package on game theory (containing functions that tests whether a game is perfect recall, finds optimal strategies for a player in a game, etc.) and would like people to ...
7
votes
1answer
145 views

Can one specify a “computed optional value”?

I would like to write a function with an optional argument, which if absent should not be a default value, but rather a computed value. What I mean is the following [warning this code does not work, ...
7
votes
3answers
1k views

Problems using hand-made transformation functions

I experience difficulties when using custom transformation rules. Below I give two concrete problems. I am interested in fixing each of them and in understanding what is wrong with my attitude to the ...
7
votes
5answers
330 views

How to generate all commutative pairings of list elements?

Given a list with an even number of elements, e.g. list = RandomSample[Array[e, 20]]; how can one generate a list of all different commutative pairings of the ...
7
votes
3answers
480 views

How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the <...
7
votes
2answers
638 views

Defining Associated Stirling Numbers of the Second Kind

First, I'm new to Mathematica. I spent my undergraduate programming with Maple, and my computer crashed and I lost it. Fortunately, my university offers free Mathematica downloads to students, so ...
7
votes
2answers
2k 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 $q[t]$...
7
votes
1answer
2k views

How to find the smallest root

I have a continuous, differentiable, monotonic, bounded function called F[t]. If t -> Infinity then ...
7
votes
2answers
174 views

Specific function for calendars

I'm creating a way to organize some daily appointments. I am a teacher and I have groups of students rotating for short periods. Sometimes 2 days a week, other times 3 days a week, ... I am creating ...
7
votes
2answers
129 views

What is the advantage of using a function with variable number of arguments whereas taking a list

I recently asked a question on which I need to use a function with variable number of arguments. But as I am trying to solve my problem I just wondered : what are the advantages of using a function ...
7
votes
3answers
3k views

Creating the Nabla operator (also known as Del operator) as an operator

How can I define the nabla operator (also known as Del operator) as a an operator, acting on everything to the right of the operator! Also taking \[Del]^2 would ...
7
votes
2answers
5k views

Building matrices by attaching vectors (columns) and matrices

Having the need to attach a column to a matrix or to join matrices to make longer rows is an operation that I use very frequently and I find the Join function ideal ...
7
votes
2answers
7k views

How to find (numerical) value of a derivative at point?

I have the following function: f[0, 0] = 0 f[x_, y_] := Exp[-(x^2 + y^2)^(-1)] How do I find its partial derivatives at any given point, including $(0,0)$? This ...
7
votes
2answers
198 views

Account for powers of 0 and 1 when defining function

I have a function that I am defining as (assume m,n,a,b are non-negative integers): myfcn[x_^m_ p_^n_, x_^a_ p_^b_] := basefcn[m, n, a, b]; and then extending to ...
7
votes
1answer
201 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: ...
7
votes
2answers
220 views

Randomly generated coordinates

I want to code that will do the following: Write a Mathematica function called randomHop that takes as its argument a list containing two coordinates x and y; ...
7
votes
1answer
71 views

How to obtain the function of a plotted Bezier curve?

I have some control points and I could just plot the Bezier curve, but I also need to have the function! How can I get the exact function of the plotted Bezier curve?
7
votes
2answers
152 views

How to create Functions that have Compiled functions effectively

I'm about to build a package that will provide lots of simple functions. I want these functions to run as fast as possible when evaluating numerically but still provide symbolic results when needed. ...
7
votes
2answers
625 views

How to define a complicated function inside the body of Compile?

I want to compile a function in a way to keep its memory footprint down. In the example below, I am trying to compile a function f that makes three calls to ...
7
votes
3answers
484 views

How to organize expression by symbols (like Collect), but apply different functions to each coefficient

I want to mimic the functionality of Collect[expr, {vars}, func], but with the following modification: The function f that is ...
7
votes
3answers
171 views

TagSetDelayed on a function with a delayed value

I'm trying to automatically optimize cases when the user evaluates Length[returnsList[x_]] to instead use returnsLength: ...
7
votes
1answer
237 views

How can a function know in which function it was evaluated?

Hello my second question here might be very hard. I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function ...
7
votes
3answers
295 views

What is the recommended way to define numeric function with special cases?

What is the recommended way to define a purely numeric function with special cases? Should I define many special cases with pattern matching? ...
7
votes
2answers
183 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 ...
7
votes
1answer
107 views

Why doesn't defined MakeBoxes display what is constructed in RHS?

Inspired by the @ybeltukov 's answer to this question, if I have the following polynomial poly = a^2 + (a^2 + b^2 - c^2)*x + b^2*x^2; var = x; this generates ...
7
votes
1answer
163 views

A question about two ways to use Default

Usually we define a function with default values by the syntax x_:default, but there is another technique to do this by registering a global default value with <...
7
votes
2answers
97 views

How to write a `HeldOptional` variant of `Optional` that does not evaluate its second argument?

How to specify optional arguments that take functional values made me wonder: Can we come up with a variant of Optional that allows to do the following: ...
7
votes
1answer
96 views

How is Attributes protected and yet can still be set?

The Attributes function itself has the attributes: Attributes[Attributes] ...
7
votes
1answer
131 views

Context-dependent functions

TL;DR warning! My question is not about getting the length of the ticks correct (although if someone has good suggestions, that would be welcomed). My question is about constructing functions that ...
7
votes
1answer
621 views

Functional-Style Partial Application and the Accumulator Pattern?

Following up on Functional-Style Fixed-Length Queue Object?, I want to generalize it to a functional-style accumulator pattern. Lots of things look like accumulators, loosely defined as binary ...
7
votes
3answers
628 views

Getting Arg[z] to go from $0$ to $2\pi$

I'm defining branch cut functions, and I'm using $\arg(z)$ as a building block. So I just spent an hour at the whiteboard assuming that $\arg(z)$ goes from $0$ to $2\pi$, and then I implement the code,...
7
votes
1answer
285 views

Creating a Block from a list of rules

How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments? The function needs to be ...
7
votes
1answer
454 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 ...
7
votes
2answers
404 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 ...
7
votes
2answers
147 views

How to make Orthogonalize simplify each new vector before using it to orthogonalize the next one?

I'm trying to construct a set of orthogonal polynomials, starting from a specially-prepared initial polynomials: ...
6
votes
3answers
779 views

How can I rewrite this with NestList or FoldList?

The original code used For: ...
6
votes
2answers
1k 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 ...
6
votes
5answers
392 views

Appending _?NumericQ automatically to every variable of a function in the definition [duplicate]

I have a list of expressions as follows: as=Table[a[k],{k,1,500}] I would like to append _?NumericQ to every element of this ...
6
votes
3answers
127 views

How to avoid repeated pattern tests in function definitions

When defining some functions which depend on many arguments, sometimes we need to include predicate constraints (?xxxQ) to reduce processing time. My question is simple: is there a way to shorten a ...
6
votes
4answers
232 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 argument <...

1 4 5 6 7 8 35