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
9
votes
1answer
407 views

Two identical functions give different answers

I am new to mathematica and I hope this is a simple problem. I have two identical functions that only differ by the variable names: ...
9
votes
0answers
204 views

What is the best and most efficient way to curry?

Suppose I have a function with a signature like g[expr, _?(f), {1}, Heads -> False] and I want to access it as an operator by giving the first argument last. I ...
9
votes
0answers
113 views

Fast Destructuring

This might be a XY problem. I first post my immediate question and some background below. Question: What's the fastest way to pass arguments matching ...
8
votes
4answers
2k views

Generating list of alphabet letters

I want to create a list of length n with the letters of the alphabet. For example, if n = 3, by list would be ...
8
votes
5answers
5k views

Find function inverse

I'm trying to find the inverse of a function: (30*x^2 (1 - x)^2) (* where 0<x<1 *) I tried all the following options: 1. ...
8
votes
2answers
9k views

Creating custom functions with multiple arguments

I am hoping that this isn't a stupid question so feel free to vote it for closure. Google failed somehow. I want to define a custom function (more complicated than the usual ...
8
votes
4answers
295 views

Generating date ranges

How can I, in one line, generate all the dates with a constant day difference from one date to another? For example, if I'm interested in dates in the interval staring from 24.4.2012 till 2015 and a ...
8
votes
4answers
328 views

How to inject unevaluated list of Unique into a RuleDelayed?

I am writing a function that constructs a rule which has Unique[] in the RHS. Here is a simplified segment of code ...
8
votes
3answers
481 views

How can I create currying functions using pure function syntax?

For example how can I write In: pureFunctionSyntax[myF] /@ {7, 3} Out: {myF[7, Log[7]], myF[3, Log[3]]}
8
votes
2answers
3k views

Define a function with a function for an input

I want to create the functions $even[f]$ and $odd[f]$ which will apply the formula $even[f(x)] = \frac{f(x)+f(-x)}{2}$ and $odd[f(x)]=\frac{f(x)-f(-x)}{2}$ for an arbitrary $f$. I tried using <...
8
votes
4answers
3k views

Force function to make assumptions about its input variables in Mathematica

I'm trying to force a Mathematica function to make an assumption about its input variables. In my case, I'm trying to define a function to return the pdf of a Gaussian. But let's use a simple toy case:...
8
votes
2answers
604 views

return {} when the function is given {}

I have a function taking an array as argument and giving an output. However, sometimes my argument is {} (the empty array), then how can I get an output {} when my argument is {}? Actually, I want the ...
8
votes
3answers
1k views

Composition of functions

I have a number of rotations computed by rot = RotationTransform[theta, point], and I would like to compose them to produce one function that is the composition of ...
8
votes
1answer
228 views

How to efficiently write code with many functions that have variable number of iterators/arguments/

I am finding myself having to code functions like this: $$\text{answer} = \sum_{k_0 = 0}^{\color{Blue}n_0} \sum_{k_1=0}^{\color{Blue}n_1}\cdots\sum_{k_{N-1}=0}^{\color{Blue}n_{N-1}} \frac{P_{k_0,k_1,\...
8
votes
2answers
755 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$...
8
votes
4answers
200 views

Elegant Replace Hold combination?

I would like to define an distributive function like this: com[x__Plus, y_] := c @@@ Distribute[c[x, y]]; which is Ok. I know that it's possible to define it ...
8
votes
2answers
367 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: ...
8
votes
1answer
468 views

How can I define a function that can accumulate its parameter?

For example, write a function add: add[3] return 3 add[4] return 7 add[10] return 17
8
votes
2answers
459 views

Can we make a faster Boole implementation?

I just learned about Mr. Wizard's very efficient code here. ...
8
votes
2answers
565 views

Creating a pure function that does operations with pure functions

Suppose I have two functions defined as follows: a = #+3& b = #^2 + 3& So when I call a[2] and ...
8
votes
2answers
1k 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. I.e.:...
8
votes
2answers
196 views

How can you Unset the following UpValues?

This expression is taken from a talk given by the late Robby Villegas. The UpValues to some symbol a is defined below as: ...
8
votes
2answers
351 views

Custom Division with lists - divide by zero

I have some lists of data that I need to divide by each-other, but my problem is that for some values the denominator is zero. Now these particular values are not very important, but I still need to ...
8
votes
2answers
601 views

Why won't this compiled function be inlined?

I've been trying to use Compile to get some code to execute more quickly, because I will need to call it millions of times. I've been using the ...
8
votes
4answers
306 views

Identify the independent variable in an expression [duplicate]

Suppose that you write something like this for your students. This is just the beginning of something I would like to write to help them with the formal definition of a limit, but I am puzzled by the ...
8
votes
2answers
345 views

Error computing sum of sum of digits

I've defined a function that computes the sum of the base-b digits of n: DigitSum[n_, b_] := Total[IntegerDigits[n, b]] Then I defined a function that computes ...
8
votes
4answers
189 views

Implementing delay in NestList?

The traditional iterated map can be implemented with NestList, such as the Logistic Map, x[n+1]=rxn, can be implemented with the single line: ...
8
votes
1answer
695 views

define a function with optional arguments

I have defined a function as follows: ...
8
votes
2answers
369 views

How to correct this, `f[a_] := FindRoot[eq == 0, {x, 0.5}]`, where `eq` contains a parameter $a$, without using `eq[a]`?

I want to solve an equation with a parameter $a$, and define the result as a function of $a$. It is something like this: ...
8
votes
2answers
464 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: ...
8
votes
3answers
392 views

How can I break through Function's HoldAll attribute to force evaluation of a body element?

The question concerns creating a function. Let consider the following code With[{f = Function[t, Cos[t]]}, {First[#], f'[#]} & ] which returns ...
8
votes
1answer
165 views

How can I use a loop with an iterator in order to evaluate a function?

I'm trying to evaluate a function, that depends on two variables, in a specific set of values. I will write an example, but my system is more complicated. ...
8
votes
1answer
2k views

What is -(-2,3,4)?

I was trying to define a function f such that, when its second argument is negative, we have $$ f(a,b,c,d,\dots,n)=f(a,-b,-c,-d,\dots,-n) $$ i.e., we reverse the ...
8
votes
3answers
193 views

Sum all columns from a list

I have a list of column names: list = {"col1", "col2", "col3"} and a dataset ds. I want to add a new column to ...
8
votes
1answer
488 views

Package functions and symbolic calculations

I'm writing my package (.m) in Mathematica and when I call the function it returns 0 instead of a right output. Here's my code: Package.m ...
8
votes
3answers
491 views

Why these ways of defining a function with variable number of arguments fail?

First: I know about the correct way to define a function with variable number of arguments: Clear[f] f[s__] := Plus[s] f[1, 2, 3, 4] (* 10 *) BUT I ...
8
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 ...
8
votes
2answers
462 views

Optional arguments with lists as function parameter

I'm developing a function f which should support optional arguments {x_, y_} in list form. A simple example looks like this: <...
8
votes
3answers
4k 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 \left(\textstyle{\int}v\...
8
votes
1answer
175 views

How to find the position in original list ranked first by SortBy

SortBy is a wonder and convenient way to sort a list using a list of functions with which ties can be broken. ...
8
votes
3answers
326 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: ...
8
votes
2answers
194 views

Possible to scope an anonymous function within Module?

This is my first question. I'm fairly new to Mathematica and completely new to SE, so I apologize if this question is trivial. I'm working on a problem that requires a function to be redefined ...
8
votes
1answer
173 views

Automatic Piecewise Approximation

I would like to create the following function: It should approximate a function func[x] by piecewise polynomial interpolations, splitting the approximate function ...
8
votes
1answer
311 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 (...
8
votes
1answer
184 views

How does a construct like `i : func[arg_] := i = an expression using arg` work? [duplicate]

Occasionally I run into a Pattern that looks like i:func[arg_]:=i= an expression using arg I have two questions on this: Can ...
8
votes
3answers
162 views

Defining a custom Plus for a specific class of arguments

I'm trying to develop an domain-specific language with Mathematica. I want to define a customized behavior for the Plus function when applied to my data. For ...
8
votes
1answer
170 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: ...
8
votes
1answer
126 views

Maximum number of literal values for SetDelayed?

Under what circumstances should I expect Mathematica to turn a delayed assignment into an immediate assignment? I have put together a minimal example (available here) which demonstrates this behavior. ...
8
votes
1answer
189 views

Position-based Inline syntax for Curry

I would like to update an existing pseudo-curry function: • /: h_[pre___, •, post___] := Function[expr, h[pre, expr, post]]; that I use inline like this - the ...
8
votes
0answers
157 views

KeyValuePattern specificity in DownValues

Bug introduced in 10.4.0 and persisting through 11.3.0 CASE:3824590 [...] Developers are discussing this behavior. [...] I will let you know when I have definite information on this. [...] ...

1
3 4
5
6 7
38