We’re rewarding the question askers & reputations are being recalculated! Read more.

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
13
votes
4answers
773 views

How to make a custom Echo

I usually check my intermediate variable by Echo.Like Table[Echo@x, {x, 10}] But when the intermediate variable have too much ...
13
votes
1answer
2k views

How does a Pringle lose its curvature?

Nom! As part of a bigger project, I've was writing some code to calculate the scalar curvature of surfaces of the form $z = f(x,y)$. This uses a general calculation of the scalar curvature to produce ...
13
votes
2answers
232 views

How can one write a robust ListableQ function?

I would like to have a test that determines if a particular function is Listable. In the case of Symbols this is merely a matter of checking Attributes. ...
12
votes
7answers
4k views

Recursive function with if-statement

I am trying to represent the following function definition in Mathematica: $$\begin{align*} f(1)&=1 \\ f(2n)&= \begin{cases}f(n) & \text{if}\space n\equiv0\pmod{2} \\ 2f(n) & \text{if}...
12
votes
4answers
603 views

Optional argument that can be completely omitted?

I am writing a simple function return similar words. Clear[similarWords] similarWords[string_]:=Nearest[WordList[],string] I want to add another argument ...
12
votes
4answers
2k views

Building a Noisy Function

Here's a nice function with a nice plot: f[t_] := Exp[-Abs[t]] Sin[t]; Plot[f[t], {t, -10, 10}, PlotRange -> All] What I would like to do is to make it noisy.....
12
votes
3answers
3k views

How do I designate arguments in a nested map?

Say I have two lists, list1 = {a, b, c} list2 = {x, y, z} and I want to map a function f over them to produce ...
12
votes
2answers
441 views

Manipulating OptionsPatterns[]

How do you combine OptionsPatterns? For example, I would like to have a function that can take both options that I specify, and options for all graphs: ...
12
votes
4answers
787 views

How to pass a list of arguments into HoldAll

I have a list of arguments (which in reality is lengthy): arguments = {a, b, c} arguments2 = {a_, b_, c_} f[Sequence@@arguments2] := a + b + c Note: It seems ...
12
votes
3answers
281 views

Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
12
votes
3answers
713 views

How to check the validity of an option value

I have a user-defined function, func, which takes the following form: func[arg1, arg2, options] Here is the actual code ...
12
votes
4answers
459 views

Equivalent Nested Loop Structure

Consider the following examples: ...
12
votes
1answer
181 views

Is it possible to set multiple type to a variable?

Consider this f[x_Real] := 2 x f[1] f[1] f[1.0] 2.0 I want ...
12
votes
2answers
1k views

How to plot a barycentric line

I want to plot a barycentric function on an equilateral triangle (ternary plot). For example f1 = {Abs[Sin[x]], Mod[x, 2], Abs[Cos[x]]}; At the moment I evaluate ...
12
votes
1answer
830 views

Safe way to put a pattern on the right hand side of a rule

I'm interested in creating a function or rule that generates its own set of rules. For instance, it would take as an input the expression h[m, n] and the indices <...
12
votes
3answers
310 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] ...
12
votes
5answers
310 views

Implement settable properties

I am looking for advice on implementing the following. (It is possible that the answer is that it is simply not a good idea to do this.) I would like to have an object similar to ...
12
votes
2answers
211 views

Set Attributes -> Orderless for only some variables in a function? [duplicate]

I have the following function: ...
11
votes
4answers
399 views

Is there a concise form of Which or similar conditional?

Suppose that I want to write a function fun that takes an Integer num as input, and returns <...
11
votes
9answers
1k views

Number of sign flips in list?

Given a list of signs: list={1,1,-1,1,-1,-1,1,1,1,-1}; how to most conveniently and quickly determine the number of sign flips in the sequence? I have this ad-...
11
votes
2answers
1k views

Number of digits for factorial of 12345678987654321

What is the number of digits (IntegerLength) of the factorial of 12 345 678 987 654 321? The number of zeros at the end of this factorial was calculated and it is huge: exactly 3 086 419 746 913 569 ...
11
votes
4answers
675 views

How to Thread with custom functions? Thread vs Map?

This function finds n given the nth prime: findPrime[n_] := If[PrimeQ[n], i = 1; While[Prime[i] < n, i = i + 1]; i, False]; This works: ...
11
votes
3answers
542 views

Where in the documentation can I find a list of function argument types?

Recently, I learned that it is possible to assign "types" to function arguments in the definition of a function. Suppose I have a function stringFun that does some ...
11
votes
2answers
4k views

Gram-Schmidt Process for Polynomials

I want to implement the Gram Schimdt procedure to the vector space of polynomials of degree up to 5, i.e. I want to find an orthogonal basis from the set of vectors $v=(1,x,x^2,x^3,x^4,x^5)$. The ...
11
votes
2answers
145 views

Left-associative Apply

Often I want a version of @ that is left-associative (might not be using that term correctly). What I mean is I'd like some operator ...
11
votes
3answers
260 views

List of functions to list of parameters

I am working in a Dataset and I need to create a function for a ChartStyle option of a ...
11
votes
3answers
555 views

How to set a TraditionalForm output for a symbol

How do I set a TraditionalForm output for a particular symbol/function? In particular I would like my user-defined symbol ...
11
votes
2answers
404 views

Strange behaviour of Check

...
11
votes
5answers
512 views

How to get the parameter number of inbuilt function

I want to make a custom function parameterNumber,which can return the parameter number of inbuilt function,as the Plot's ...
11
votes
4answers
537 views

Using predefined expressions as free variables in a pure function [duplicate]

I want to have a function value of an expression where some variables are solutions to some set of equations, with some values of parameters. I had an idea to use pure functions for that. However, ...
11
votes
5answers
1k views

Reverse Polish Notation

Is there a way to evaluate a string containing RPN in Mathematica? SE thinks this question is too short, so let me expand on it. Do you know of any function, that provides the following functionality?...
11
votes
2answers
474 views

How to build private function with built-in function's option

Since this code work well: ...
11
votes
2answers
239 views

Simplifying a reverse Fold for a Query

Is there a simpler way to parametrize this tail-recursive expression to be applied as a Query (for processing a nested ...
11
votes
1answer
193 views

Is there a built-in function to inverse Thread?

Namely, while Thread @ f[{a, b}, {x, y}] gives {f[a, x], f[b, y]} the InverseThread (if it existed) should reverse the ...
11
votes
1answer
557 views

Inheriting Options?

This question is an extension of Passing down arguments. To help clarify my question, I will be referring to another recent post of mine Making Quartile Plots from a Dataset, the code to which can ...
11
votes
1answer
176 views

Syntax information for pure functions

Mathematica's help on SyntaxInformation does not rule out syntax information for pure functions. SyntaxInformation[f] gives information used to generate syntax coloring and other advisories when f[…...
11
votes
2answers
476 views

Combining pure functions

Is there an easy way to combine pure functions into a single pure function? For example, say I have f = #1^2 &; g = #1 - 2 &; and I want to define a new ...
11
votes
1answer
236 views

Copy Built-in function to new name

It is know that some built-in functions are system function and changing their properties may cause trouble. Suppose for example I want to change features of Cases: ...
11
votes
2answers
300 views

can a ColorFunction have side effects?

Can anyone explain this (to me) odd behavior. If I run this code once (fresh kernel) it works as intended. ...
11
votes
1answer
909 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 ...
11
votes
1answer
333 views

Fine tuning compiled code that computes dilogarithm function

As an exercise in writing a good Compile function, I want to do the simple task of coding a routine that outputs the real part of the dilogarithm function ...
10
votes
5answers
413 views

Function for a series of joined slopes

I need a function for a series of joined slopes and my solution feels a bit kludgy. Is there a better way? A list of pairs of transition points and slopes: ...
10
votes
7answers
719 views

intersection between two 2D arrays with labeled data is slow

Here is how to Generate data the function is below with two images to generate data img1 img2 ...
10
votes
2answers
752 views

Functions that remember their values

Can someone explain what is going on with the following ... func[y_] := func[y] = (Print["Hello world !!!"];) func[1] Hello world !!! ...
10
votes
3answers
452 views

How to exit from the innermost enclosing pure function?

Here is what I tried to do: Scan[Function[x, Scan[Function[y, If[y == 2, Return[]]; (* Do something useful *) Print[y]], x]], {{1, 2, 3}, {4, 5, 6}}] ...
10
votes
6answers
401 views

Combinations of multiple matching patterns

I wonder if there is any nice way to combine DownValues (or any other suitable rule-/pattern-/function -based Mathematica construct) when multiple patterns match an expression. Let me explain what I ...
10
votes
2answers
417 views

Name rewriting for Function fails in closure

At its heart, Mathematica is a dynamically scoped language. While the choice of dynamic scope for Mathematica is very much a defensible one, lexical scope is too useful to do without, and Mathematica ...
10
votes
2answers
6k 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 1....
10
votes
4answers
187 views

Efficiently merge lists chronologically without duplicates?

Prepare some dummy data to investigate: ...
10
votes
1answer
365 views

Question about Rules and Pattern (The operator patt/;test)

Maybe this question is so trivial but it has confused me. I'm studying the What the @#%^&*?! do all those funny signs mean? and in the Rules and patterns under ...