# 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.

1,860 questions
Filter by
Sorted by
Tagged with
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 ...
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 ...
192 views

### Efficiently merge lists chronologically without duplicates?

Prepare some dummy data to investigate: ...
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 ...
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[] ...
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 ...
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: ...
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 ...
1k views

I'm looking for a robust way to add vertices to a graph by modifying its AdjacencyMatrix. Here's what I have so far: ...
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 ...
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-...
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 ...
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: ...
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} ...
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 ...
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 ...
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 <...
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 ...
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 ...
872 views

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 ...
105 views

### Except @ OptionsPattern[] affects OptionValue

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

### Finding large prime gaps

I am using basic searches for prime gaps as follows: ...
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. ...
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, ...
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[] ,...
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 ...