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
6answers
457 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
426 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
4answers
192 views

Efficiently merge lists chronologically without duplicates?

Prepare some dummy data to investigate: ...
10
votes
1answer
410 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 ...
10
votes
2answers
540 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
506 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
1k 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
1answer
448 views

Define a New function like Log

I want some help here. I want to define a function called Lg[z] that has the property that Lg[a/b] is always converted automatically to the form Lg[a] - Lg[b] , when a and b are polynomials of some ...
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
205 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
269 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
253 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
110 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
2k 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
176 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
1answer
151 views

Is Sow/Reap compatible with Composition?

I'm trying to figure out why these two pieces of code return different results. As expected, f[Reap[Sow[x]]] gives ...
10
votes
2answers
498 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
166 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
183 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
872 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
267 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
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 ...
9
votes
6answers
494 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
802 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
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
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
2answers
380 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
4answers
2k 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
444 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
124 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
120 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
3k 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
8k 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
1k 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
2answers
124 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
1answer
294 views

Force evaluation of user-defined function

Consider a function that sorts its arguments based on a function called order ...
9
votes
1answer
207 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
157 views

Element–wise summation for custom function

I use a custom function/head Operator[low, high] which encodes a generic polynomial expression split into "low" and "high" summands following considerations which are irrelevant to this question. Two ...
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
176 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
105 views

Except @ OptionsPattern[] affects OptionValue

The background This example works as expected, small function with a special case rule: ...
9
votes
1answer
299 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
257 views

Finding large prime gaps

I am using basic searches for prime gaps as follows: ...
9
votes
2answers
289 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
204 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
346 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
320 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
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: ...

1 2 3
4
5
38