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
10
votes
2answers
501 views

Using up-values and down-values

I have a problem I'm trying hard to find a solution for, although I don't know if a solution exists. The following rules produce the result e[2].f[] ...
10
votes
2answers
478 views

Define Log so that negative reals evaluate on lower edge of branch

I need to get Mathematica to evaluate the logarithm of a negative real number using the lower branch instead of the upper branch, so that while ...
10
votes
1answer
821 views

What is the elegant way of applying function to two lists?

I am interested in finding the most concise elegant way of doing the following: We have a function fun[x1,x2] and two lists: ...
10
votes
2answers
1k views

Proper way to add vertices to an adjacency matrix

I'm looking for a robust way to add vertices to a graph by modifying its AdjacencyMatrix. Here's what I have so far: ...
10
votes
2answers
186 views

Why a function returns two different values if copied into a Notebook or imported with Get from a Wolfram Mathematica Package?

I have some Mathematica code that shows a behaviour I really cannot understand. The code only contains a bunch of function definitions. If I paste it in the Notebook Front End and I evaluate it, I ...
10
votes
2answers
219 views

How to make `D` apply chain rule on user-defined symbols as it does on `Dot` and `NonCommutativeMultiply`

I have my own version of Dot called mydot, and it behaves quite similar to the built-in version in that it represents the non-...
10
votes
1answer
246 views

Is it possible to restrict the arguments for a function to a specific entity type?

I'm trying to wrap my head around how to work with Mathematica for building (computable) domain specific knowledge. One particular thing I didn't quite understand is how to create symbols for ...
10
votes
2answers
100 views

Extract DownValues used in function evaluation

Is there a convenient way to find out which DownValues is used when calling a function? As an example, consider the following function: ...
10
votes
2answers
1k views

Mapping data on Archimedes' spiral

I would like to map my data on Archimedes' spiral and preserve the distance between points on the curve. The test data consists of 20 evenly spaced {x,y} ...
10
votes
1answer
173 views

How to get a unevaluted expression from a compressed string?

Of course we know Defer[Range[20]] Range[20] But I try to compress my expression into a string ...
10
votes
2answers
489 views

Defining a ForEach function

Consider the following function definition: ForEach[iterators__] := Table[#, iterators] &; Two questions about it: I. It works perfectly if you call it like <...
10
votes
1answer
161 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 ...
10
votes
1answer
741 views

Is there a function to generate a minimal clique edge 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 $V=\...
10
votes
1answer
157 views

Is there a way to TagSet a HoldAllComplete function?

Let's say I want to overload the built-in function Lookup, to handle my data wrapped in myObject. One would use ...
10
votes
1answer
261 views

How to deal with bad arguments when a function's UpValues is a pure-function?

In practice, I needs to define the UpValues of a user-defined function. For instance, the operation of function like differential formula , expansion and so on. ...
9
votes
6answers
492 views

How to generate this structure of a list without `While` and the like?

I need to create a list that holds, for given integer d, the elements $1,\ldots,d,d+2,\ldots,2d,2d+3,\ldots,3d,3d+4,\ldots,d^2$ where $\ldots$ is just denoting ...
9
votes
4answers
781 views

Generating a position lookup function for an arbitrary list of integers

In my code, I generate a list of integers called numlist. Here is an example numlist: ...
9
votes
2answers
430 views

multiple reflections of a laser beam in a triangle

I want to make a animation that multiple reflections of a laser beam in a triangle looked like, I have tried following, but it's not a good way, I'm looking for a better way(Nested solution). ...
9
votes
4answers
2k views

How make f[{x,y}] evaluate as f[x,y]? [duplicate]

I frequently encounter the situation where I have a function of two real variables defined, e.g.: f[x_, y_] := 9 - x^2 - y^2 But then I need to feed into ...
9
votes
2answers
369 views

Using vtx[] instead of vtx

There're two ways to generate a random tree as 1 and 2 below. Both work fine here. So what's the reason to use vtx[] instead of ...
9
votes
3answers
1k views

Inverting a function in a certain region

InverseFunction works well for globally invertible functions, like f = 2*# + 2 &; InverseFunction[f] ...
9
votes
4answers
443 views

Constructing a function with Flat and OneIdentity attribute with the property that otimes[a]:>a

I want to have a function (called otimes) with the following two properties: it has the attribute OneIdentity and ...
9
votes
4answers
1k views

Function argument to default under certain condition

Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met? For example, if a function is defined as: <...
9
votes
2answers
346 views

Duplicate the swap function

Because defining the function: swap[x_, y_] := Module[{}, t = x; x = y; y = t; Return[]] and writing: swap[x, y] {x, y} I ...
9
votes
4answers
114 views

How to add Optional Sequence

Note: This question could be a duplicate but I couldn't find any so far. It is known that Optional (:) can be use to represent expression if omitted. ...
9
votes
2answers
119 views

Can definitions for functions be written accepting alternatives for arguments

New to mathematica and also not entirely sure how to phrase my questions but basically I have a function I'm trying to write where several inputs will return the same value. For example, ...
9
votes
1answer
2k views

How to split text without spaces into list of words? [duplicate]

What is the best approach to a problem like this in Mathematica? I can only manage this: ...
9
votes
2answers
7k views

Run entire notebook

I have created a notebook, with functions. The only thing that I want to change in the notebook later is the input file. I therefor want to have a button that runs every input step-by-step so that I ...
9
votes
2answers
121 views

For loop works at top-level but not in a function

Why does this work: For[i = 1, i <= Dimensions[list][[1]], i++, list = Insert[list, i, {i, 3}]] But this doesn't: ...
9
votes
2answers
947 views

How can I get the right hand side of a delayed expression?

Imagine there is a given function f, defined with SetDelayed, say f[x_] := Sin[x]^2 + Cos[x]^2. Is it possible to get rhs of ...
9
votes
1answer
219 views

Force evaluation of user-defined function

Consider a function that sorts its arguments based on a function called order ...
9
votes
1answer
199 views

Map at a subsequence of a list

I'm trying to construct a function somewhat similar to MapAt. A basic usage example should look like this: ...
9
votes
1answer
277 views

How to define function that can change patterns

Given time series data as follows: data = {{{2011, 1, 13}, 10}, {{2011, 2, 14}, 17}, {{2011, 3, 15}, 20}, {{2012, 1, 16}, 30}, {{2012, 2, 17}, 40}}; ...
9
votes
1answer
174 views

How to make a function to analyse a sentence?

I have two lists: grammarNonterminal = {{VP,{V,NP}},{NP,{D,N}},{S,{NP,VP}}} sentence = {{D,"the"},{N,"man"},{V,"hit"},{D,"the"},{N,"table"}} I want to make a ...
9
votes
2answers
1k views

Defining the Moyal Product in Mathematica

What would be the best way to define the Moyal Product, $f(x,p)\star g(x,p)$, in Mathematica? $f(x,p)\star g(x,p)$ may be written as $f\star g=\sum\limits_{n=0}^{\infty}\frac{1}{n!}\left(\frac{i\hbar}...
9
votes
1answer
1k views

Functional programming and Do loops

I'm writing a Monte Carlo simulation. I'm using a Do loop to create the different simulations, iterating over different ...
9
votes
1answer
101 views

Except @ OptionsPattern[] affects OptionValue

The background This example works as expected, small function with a special case rule: ...
9
votes
1answer
262 views

Maximizing a recursive functional equation

I would like to maximize a recursive functional equation but I am struggling with setting up the problem correctly. The equation I am interested in captures the nature of a decision process over time ...
9
votes
1answer
250 views

Finding large prime gaps

I am using basic searches for prime gaps as follows: ...
9
votes
2answers
264 views

using memoization with conditional statement

I would like some advice about the use of memoization in conjunction with conditional statements. Let me try to explain my problem: I am constructing a function depending on a number of parameters. ...
9
votes
1answer
184 views

Why there is only `HoldFirst` but no `HoldSecond`, `HoldThird`, …`HoldN`?

I feel that this must be a duplicate. But I just can't find such a post. Mathematica has HoldFirst, HoldRest, ...
9
votes
1answer
334 views

How to build a function object like the built-ins such as Interpolation?

There are many built-in functions that return a function object, such as Interpolation[], BSplineFunction[] ,...
9
votes
2answers
310 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
9
votes
1answer
394 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
108 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 ...
9
votes
0answers
149 views

Does Compilation option “InlineExternalDefinitions”/“InlineCompiledFunctions” lead to unnecessarily complicated nesting?

Question: When an external (possibly complicated) definition f is used inside a compiled function with ...
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
3answers
4k views

Better solution than returning a list of 3 values?

I have a function (using SetDelayed) that currently returns 3 values in a list. Later on I use the result of this list along with ...
8
votes
5answers
4k 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
4answers
293 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 ...