Questions tagged [downvalues]

Questions about DownValues which specify function definitions for symbols.

Filter by
Sorted by
Tagged with
0 votes
1 answer
38 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): ...
user avatar
  • 195
0 votes
0 answers
34 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, ...
user avatar
  • 1,448
4 votes
1 answer
98 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_ ] := ... ...
user avatar
  • 928
1 vote
1 answer
30 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. ...
user avatar
  • 1,055
4 votes
2 answers
90 views

Assignment with Span to Indexed Variable

If I use span to assign a value to every element in a "column", it miraculously works: ...
user avatar
  • 1,393
4 votes
2 answers
156 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.
user avatar
  • 687
2 votes
1 answer
54 views

Downvalues for compiled functions

I'm a noob when it comes to DownValues, UpValues, OwnValues, etc. but I figured I would give ...
user avatar
  • 413
0 votes
0 answers
18 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 ...
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} *) ...
user avatar
  • 1,055
3 votes
1 answer
100 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 ...
user avatar
  • 1,117
1 vote
1 answer
46 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. ...
user avatar
2 votes
1 answer
90 views

Issue using optional argument before options [duplicate]

I have a problem using optional arguments before an OptionsPattern: ...
user avatar
  • 30.2k
8 votes
0 answers
84 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. ...
user avatar
  • 30.2k
1 vote
0 answers
116 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 ...
user avatar
  • 429
14 votes
3 answers
235 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 ...
user avatar
  • 1,094
2 votes
2 answers
67 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
48 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 (...
user avatar
  • 13
6 votes
2 answers
239 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 ...
user avatar
  • 6,328
1 vote
0 answers
42 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 ...
user avatar
  • 8,075
2 votes
2 answers
54 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 ...
user avatar
2 votes
0 answers
57 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: ...
user avatar
  • 30.2k
10 votes
2 answers
193 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, ...
user avatar
  • 616
4 votes
1 answer
196 views

Sorting a list of rules

When we give several definitions for a function f, the DownValues of f are automatically ...
user avatar
  • 9,839
4 votes
2 answers
220 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, ...
user avatar
  • 635
1 vote
1 answer
82 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 ...
user avatar
  • 822
11 votes
1 answer
224 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 <...
user avatar
  • 771
6 votes
1 answer
587 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 ...
user avatar
  • 14.7k
4 votes
2 answers
124 views

Assigning values to association in DownValues

Compare the following code: Clear[t]; t = <||>; t[2] = 1/2; t[2] with ...
user avatar
2 votes
2 answers
120 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 ...
user avatar
  • 987
8 votes
0 answers
162 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. [...] ...
user avatar
  • 132k
0 votes
1 answer
74 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 ...
user avatar
  • 183
3 votes
1 answer
508 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: ...
user avatar
  • 411
0 votes
3 answers
521 views

How to initialize downvalues from a list?

The For expression below initializes downvalues of a variable a from the values in a list ...
user avatar
  • 11.4k
4 votes
1 answer
96 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 ...
user avatar
1 vote
1 answer
104 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 ...
user avatar
  • 30.2k
11 votes
6 answers
574 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 ...
user avatar
  • 8,259
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 ...
user avatar
  • 2,195
14 votes
2 answers
478 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) = ...
user avatar
31 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 ...
user avatar
  • 28.4k
12 votes
1 answer
146 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} ...
user avatar
  • 11.3k
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: ...
user avatar
5 votes
1 answer
235 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 ...
user avatar
  • 106k
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 ...
user avatar
  • 163
4 votes
4 answers
494 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 ...
user avatar
  • 744
2 votes
1 answer
186 views

How do I constrain movement for lists of Locators using 2nd argument of Dynamic?

I need to display edge labels of a graph in a way that allows the edge labels to be moved. Locator seems the simplest and most obvious function to use. My ...
user avatar
  • 1,027
2 votes
0 answers
94 views

Function that assigns properties [duplicate]

I want to create a function that creates and assigns properties to a symbol. In theory this would behave similarly to the NonzeroValues property of ...
user avatar
  • 10.6k
5 votes
1 answer
454 views

String pattern matching in function definition [duplicate]

I have a function f and its downvalues. I want to code an extra downvalue for arguments of the form "part1:part2" I don't understand why f[a__~~":"~~b__] := {a,b} ...
user avatar
  • 1,188
7 votes
1 answer
218 views

Graphics Function Definition Style (Using SubValues vs. DownValues)

I am going through some of the items in "Modern Differential Geometry of Curves and Surfaces with Mathematica, Third Edition". An example of the style used by these authors is: ...
user avatar
  • 975
6 votes
1 answer
403 views

How can I define a ⊕ operator?

What is wrong with the following code? CirclePlus := SparseArray[Band[{1, 1}] -> {##}] & a_ ⊕ b_ := CirclePlus[a, b] SetDelayed::write: Tag Function in (...
user avatar
  • 1,095
2 votes
2 answers
402 views

How to unprotect GeneratingFunction

I'm trying to add custom downvalues for GeneratingFunction, and to do so I thought I just had to Unprotect it. However, that ...
user avatar
  • 1,116