Questions tagged [downvalues]
Questions about DownValues which specify function definitions for symbols.
70
questions
4
votes
1
answer
168
views
Why is {_Integer..} matching {_Rule..}?
Why is this getting stuck in a loop:
...
1
vote
2
answers
89
views
How to pass arguments of outer function into inner function implicitly?
Consider the following setup:
...
1
vote
0
answers
33
views
Getting DownValues from memoized function - ParallelEvaluate only uses one kernel at a time
I have the following code where I calculate primorial(n) using a memoized function. When I run this function in ParallelDo everything is fine - the calculation runs on 8 kernels.
But I want to use ...
0
votes
0
answers
43
views
Is this behaviour of pattern ordering of OptionsPattern stable and documented? [duplicate]
My version is 13.2. This behaviour of OptionsPattern together with `` is extracted from some complicated functions.
Motivation
<...
2
votes
3
answers
199
views
A simple question about syntax: how to shorten [email protected]@
Consider the following function:
...
4
votes
1
answer
100
views
Can UpValues (as well as other "*Values") always be converted to DownValues?
As this post has discussed, UpValues are certainly useful, and as this post has discussed, g /: f[g[x_]] := h[x] is not ...
2
votes
1
answer
64
views
How to assign a value for specific Indeterminate expressions?
I wanted to form a list as follows
j=1;
Table[(i-1-j)^k, {i,2j+1}, {k,-j,j}]
However, an error message occurred since the list included none positive powers of ...
1
vote
2
answers
118
views
How to organise definitions that contain named parameters?
I have lots of definitions of (usually numeric) quantities that depend on parameters; they're mostly matrices (in the actual code, there are many more definitions):
...
0
votes
0
answers
35
views
Define a new rule to take precedence over the others in a symbol's `Downvalues` list [duplicate]
I have a symbol that has a very large list of Downvalues. One of the rules in this list should be applied before the others because it simplifies many expressions, ...
4
votes
1
answer
107
views
Search speed of downvalues when indexing with strings
I'm writing a lot of print functions for different parts in a package and at the moment am using something like
myprint[ identifierString_String ][ data_ ] := ...
...
1
vote
1
answer
44
views
Saving function evaluations to DownValues for function with options
I have a time-intensive function with options that I would like to not evaluate more than necessary.
...
4
votes
2
answers
137
views
Assignment with Span to Indexed Variable
If I use span to assign a value to every element in a "column", it miraculously works:
...
5
votes
2
answers
185
views
How to make DownValues not reorder function definitions
What can I do to prevent DownValues from reordering my function definitions?Is there a simple way to fix it? Any suggestion will be greatly appreciated.
2
votes
1
answer
73
views
Downvalues for compiled functions
I'm a noob when it comes to DownValues, UpValues, OwnValues, etc. but I figured I would give ...
0
votes
0
answers
22
views
How to memoize with patterns? [duplicate]
Here is an artificial example to explain what I am up to. Define
ClearAll[f]
f[x_, y_] := f[x, y] = If[x == 0, g[y], g[f[x - 1, y]]]
Then ...
3
votes
3
answers
65
views
Return[] being saved to DownValues
Example:
f[x_] := f[x] = (If[x < 0, Return[x]]; x)
f[1];
f[-5];
DownValues[f][[1 ;; 2]]
(* {HoldPattern[f[-5]] :> Return[-5], HoldPattern[f[1]] :> 1} *)
...
3
votes
1
answer
148
views
General definitions of deeply nested functions without SetDelayed, "UpUpValues"
I'd like to assign an "UpUpValue" in a way generalized to any nested head surrounding the value for which the UpUpValue would be defined. That is, if a function ...
1
vote
1
answer
48
views
Function definitions not being applied when using replacements or lists with fractional steps
I am attempting to use Table to evaluate a function at specific values. The function is first defined.
...
2
votes
1
answer
101
views
Issue using optional argument before options [duplicate]
I have a problem using optional arguments before an OptionsPattern:
...
8
votes
0
answers
87
views
Design Patterns for System Functions?
Using PrintDefinitions give one a great view into many system functions. Often the design of these symbols incorporate multiple patterns that I see again and again. ...
2
votes
0
answers
160
views
Saving DownValues for a function computed in parallel
Is it possible to save DownValues of a function computed using a parallelization routine?
I have a rather costly function f I wish to evaluate at a grid and save ...
14
votes
3
answers
274
views
What is the difference between Set and SetDelayed when evaluating the RHS leaves it unchanged?
I thought there was no difference between f[a_] = 2 + a; and f[a_] := 2 + a; because the resulting ...
2
votes
2
answers
69
views
Correct way to see domain of function using DownValues?
Here's a simple function g defined for the natural numbers 1 through 10:
Table[g[i] = i^2, {i, 1, 10}]
I now want to find the ...
0
votes
1
answer
52
views
Cannot set DownValues to ValueList while loading package
I have installed a package named HEP from Wolfram Library Archive which contains several .m files by Files -> Install (...
7
votes
2
answers
269
views
What makes a pattern more general?
This is a rewording of the question. The original post is below.
How come that a pattern for an expression with a specific head is not more specific than a pattern for an expression with a generic ...
1
vote
0
answers
48
views
Pure OwnValue Functions vs Patterned DownValue Functions [duplicate]
Is it better to use pure Function assigned to symbols as their OwnValues vs function defined by patterns as symbol's ...
2
votes
2
answers
58
views
Defining a property of a variable that includes another function (sign)
I have a function that has a lot of sign functions that depend on a variable $\sigma$ and I know the property that $\sigma=1$ with probability $m$ and $-1$ with probability $1-m$. For example we can ...
2
votes
0
answers
60
views
How to add sugar for NetEncoder?
I'm tired of the extra level of list in NetEncoder and would like to get rid of it.
The obvious thing fails:
...
10
votes
2
answers
201
views
Unexpected behavior when directly assigning to DownValues
I'm encountering some unexpected behavior when assigning to DownValues. When I assign two definitions for test[1] as below, ...
5
votes
1
answer
277
views
Sorting a list of rules
When we give several definitions for a function f, the DownValues of f are automatically ...
4
votes
2
answers
224
views
Wrongly assigning a value to an expression
Sometimes I misstype an equality and I write an assignment instead.
For example, if I write
f'[x] = 2
instead of an equation, ...
1
vote
1
answer
87
views
Changing DownValues of a function when calling
General problem
I am looking for a possibility to store information in parallel for a function and change the information when a function is called.
Idea
It seems possible to solve this problem by ...
11
votes
1
answer
233
views
Why can I define N[MySymbol] := ...?
As far as I understand, I cannot define the values of built-in functions: Sin[Cat] := Dog will result in error.
This is because all built-in symbols have attribute <...
6
votes
1
answer
598
views
Optimize DownValues: extract "non-patterns" from Alternatives
Introduction
As we know, when we assign DownValues to a symbol, Mathematica creates a dispatch table for "non-pattern" definitions (i.e. expressions that, when ...
4
votes
2
answers
146
views
Assigning values to association in DownValues
Compare the following code:
Clear[t];
t = <||>;
t[2] = 1/2;
t[2]
with
...
2
votes
2
answers
132
views
Memoization and finding already calculated DownValue
The problem of plotting an expensive function discussed in this question caught my attention. As an exercies I tried to solve it using memoization as follows
...
8
votes
0
answers
163
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. [...]
...
0
votes
1
answer
77
views
Trying to "fix" Simplify, but cannot Up/DownValue it because it is Protected
There is an irksome shortcoming for Simplify that I encountered:
Simplify not evaluating completely with Square roots
-- still not entirely sure if it's because of AssumptionsMaxNonlinearVariables ...
4
votes
1
answer
652
views
How to "clear" the downvalues of a subscripted variable?
I have a function that sets certain subscripts of the symbol aa as in the following example:
...
0
votes
3
answers
565
views
How to initialize downvalues from a list?
The For expression below initializes downvalues of a variable a from the values in a list ...
4
votes
1
answer
119
views
Using ValueQ to test whether "hash" key exists fails when key is given as variable
Please ignore ^^^ possible duplicate (raised by me). The solution doesn't cover my case.
I'm trying to implement a simple hash table according to this top answer at "Is there HashTable structure in ...
1
vote
1
answer
124
views
How to extract all necessarily code for a function?
Many times when someone pastes a piece of code into SE there are missing values and symbols that the user left out.
To prevent this I'm looking for a function that uproots other functions (extract ...
12
votes
6
answers
753
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 ...
4
votes
2
answers
2k
views
How to take derivative of a interpolated function inside the module
When handling derivatives of a interpolated function, I often use the following function
makeDeriv[f_InterpolatingFunction] = D[ f[x], x]
Then, I can define ...
14
votes
2
answers
532
views
Named patterns in Except
Bug introduced in 8 or earlier and fixed in 10.4.0
Bug isn't present in version 5.2
I don't think I quite understand how Except works.
I want to define $f(a,b) = ...
34
votes
5
answers
2k
views
What are the requirements for a well behaved indexed variable? Subscript, ToExpression, Downvalue?
On writing this answer I needed to call a function, (NonlinearModelFit) with an unknown number of parameters.
We have learned that we should not use ...
12
votes
1
answer
157
views
Which symbol gets DownValues or OwnValues when using SetDelayed?
I have been looking at various usages of Pattern objects. Here's a number of ways to define the same thing:
mod[x_] := {x}
...
0
votes
0
answers
39
views
Why is DownValues releasing RuleDelayed's hold for Graphics? [duplicate]
I am trying to understand DownValues in regards to the evaluation of its output. Consider this:
...
5
votes
1
answer
254
views
Multiple down-values with exactly the same lefthand side
I was working on an answer to this question, when I was sidetracked by what I bring up here.
I wanted a function, which when given a string representing a binary number, would convert the string to a ...
6
votes
3
answers
3k
views
How to force 2+2=5
I was looking at this question (writing a program such that 2 + 2 = 5) and tried to write something in Mathematica, but I couldn't get something to work without completely replacing ...