Skip to main content

Questions tagged [downvalues]

Questions about DownValues which specify function definitions for symbols.

Filter by
Sorted by
Tagged with
1 vote
2 answers
82 views

How to pass arguments of outer function into inner function implicitly?

Consider the following setup: ...
user13892's user avatar
  • 9,443
1 vote
0 answers
32 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 ...
1729taxi's user avatar
  • 777
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 <...
Lacia's user avatar
  • 2,323
2 votes
3 answers
196 views

A simple question about syntax: how to shorten [email protected]@

Consider the following function: ...
John Taylor's user avatar
  • 5,827
4 votes
1 answer
99 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 ...
user688486's user avatar
2 votes
1 answer
63 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 ...
Po1ynomial's user avatar
1 vote
2 answers
115 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): ...
Deniz's user avatar
  • 235
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, ...
Diffycue's user avatar
  • 1,844
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_ ] := ... ...
Gert's user avatar
  • 1,530
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. ...
H.v.M.'s user avatar
  • 1,093
4 votes
2 answers
134 views

Assignment with Span to Indexed Variable

If I use span to assign a value to every element in a "column", it miraculously works: ...
Nicholas G's user avatar
  • 1,981
5 votes
2 answers
183 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.
Ren Witcher's user avatar
2 votes
1 answer
71 views

Downvalues for compiled functions

I'm a noob when it comes to DownValues, UpValues, OwnValues, etc. but I figured I would give ...
shanedrum's user avatar
  • 587
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 ...
მამუკა ჯიბლაძე's user avatar
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} *) ...
H.v.M.'s user avatar
  • 1,093
3 votes
1 answer
143 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 ...
Ghersic's user avatar
  • 1,157
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. ...
epsilon0's user avatar
2 votes
1 answer
98 views

Issue using optional argument before options [duplicate]

I have a problem using optional arguments before an OptionsPattern: ...
M.R.'s user avatar
  • 31.5k
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. ...
M.R.'s user avatar
  • 31.5k
1 vote
0 answers
156 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 ...
mikeonly's user avatar
  • 429
14 votes
3 answers
272 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 ...
MeMyselfI's user avatar
  • 1,116
2 votes
2 answers
68 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 ...
user avatar
0 votes
1 answer
51 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 (...
Rohan's user avatar
  • 13
7 votes
2 answers
265 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 ...
Hector's user avatar
  • 6,438
1 vote
0 answers
47 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 ...
user13892's user avatar
  • 9,443
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 ...
Capivara Cometa's user avatar
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: ...
M.R.'s user avatar
  • 31.5k
10 votes
2 answers
199 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, ...
tom's user avatar
  • 636
5 votes
1 answer
272 views

Sorting a list of rules

When we give several definitions for a function f, the DownValues of f are automatically ...
Fred Simons's user avatar
  • 10.2k
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, ...
dpravos's user avatar
  • 665
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 ...
pbx's user avatar
  • 842
11 votes
1 answer
230 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 <...
bcp's user avatar
  • 781
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 ...
jkuczm's user avatar
  • 15.1k
4 votes
2 answers
145 views

Assigning values to association in DownValues

Compare the following code: Clear[t]; t = <||>; t[2] = 1/2; t[2] with ...
William Riddle's user avatar
2 votes
2 answers
128 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 ...
ercegovac's user avatar
  • 1,017
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. [...] ...
Kuba's user avatar
  • 137k
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 ...
kozner's user avatar
  • 283
4 votes
1 answer
635 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: ...
matrixbud's user avatar
  • 435
0 votes
3 answers
564 views

How to initialize downvalues from a list?

The For expression below initializes downvalues of a variable a from the values in a list ...
kjo's user avatar
  • 11.7k
4 votes
1 answer
118 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 ...
Andrew Cheong's user avatar
1 vote
1 answer
120 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 ...
M.R.'s user avatar
  • 31.5k
12 votes
6 answers
740 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 ...
Sascha's user avatar
  • 8,469
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 ...
Sungmin's user avatar
  • 2,305
14 votes
2 answers
525 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) = ...
QuantumDot's user avatar
  • 19.7k
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 ...
rhermans's user avatar
  • 36.7k
12 votes
1 answer
156 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} ...
LLlAMnYP's user avatar
  • 11.5k
0 votes
0 answers
38 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: ...
Gustavo Delfino's user avatar
5 votes
1 answer
251 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 ...
m_goldberg's user avatar
  • 108k
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 ...
Artefacto's user avatar
  • 163
4 votes
4 answers
541 views

How to do memoization together with evaluation of a complicated function

UPDATE 2: much shorter version Given a list of paired indices {i, j}={{1,2}, {3,4}, …}, I need to first evaluate a rather complicated function test[i,j,x] (because ...
Leo Fang's user avatar
  • 754