# Tag Info

22

This is a simplification of your solution: LanguageExtendedFullDefinition[new] = LanguageExtendedFullDefinition[old] /. HoldPattern[old] :> new I believe LanguageExtendedFullDefinition is used in transferring definitions between the main kernel and subkernels. Also note the HoldPattern on the LHS of the rule which ensures that OwnValues will ...

14

Yes, UpValues are certainly useful in that you can bind definitions and custom behaviours to the symbol rather than the operator. For instance, I can define (a simple, silly example): g /: g[x_] + g[y_] := g[x] g[y] to actually multiply the two when I add them. This definition is now stored in: UpValues[g] (* {HoldPattern[g[x_] + g[y_]] :> g[x] g[y]} ...

12

Oh yes. UpValues are used quite a bit. There are several common uses, and you may have a look at this and especially this question and answers therein to see some sample uses. As for practical uses: I will just mention a couple of examples for what I consider to be the main practical use: overloading functions (system or user-defined) on custom data types, ...

12

UpSet and UpSetDelayed make multiple assignments: UpSet associates an assignment with all the distinct symbols that occur either directly as arguments of lhs, or as the heads of arguments of lhs. f[a[__], b[__], c[__]] ^:= "UpValue" UpValues[a] UpValues[b] UpValues[c] {HoldPattern[f[a[__], b[__], c[__]]] :> "UpValue"} {HoldPattern[f[a[__], ...

10

As Rojo notes, defining f/: Subscript[f,1][x_] doesn't work as f is too deep in the expression, however you could define your function in two steps to avoid tying the definition to Subscript. f /: Subscript[f, n_] := subs[f, n] subs /: subs[f, n_][x_] := g[x] This has the problem that Subscript[f,1] for instance will turn up in output as subs[f,1] rather ...

8

Does this work as you want to? SetAttributes[f, HoldAllComplete]; {first, rest___} ^:= HoldComplete[rest] f[args___] := {first, args} f[own, down[1], sub[1][2], N[n], up] HoldComplete["OwnValue", "DownValue", "SubValue", 3.14, up]

8

In answer to your first question, and as Leonid stated in a comment, you need to use TagSetDelayed to remove the ambiguity from your UpSet definition, e.g. DF /: Part[DF[expr_], r_String, rest___] := expr[[Position[expr, r][[1, 1]], rest]] I previously laid out a framework for this kind of problem, but you seem not to have used it. I shall attempt a ...

6

Slightly modifying @jVincent's idea so that if there's no match with your definition you go back to the Subscript Module[{$guard = True, subs}, f /: Subscript[f, n_] /;$guard = subs[f, n]; subs[f, n_][sth_Integer] := g[sth]; subs[f, n_][sth_] := Block[{\$guard = False}, Subscript[f, n][sth]] ] Now Subscript[f, 1][2] Subscript[f, 1][2.5] // ...

5

For read-only access to Excel data, I use a custom Set function that basically assigns an excel sheet to a symbol: setFromExcel::usage = "setFromExcel[symbol, file, sheet, opts] is a custom Set method which enriches 'symbol' for use as a data function."; skipRows::usage = "skipRows is an Option to the setFromExcel function. Use 'skipRows -> ...

5

You can do this with the Notation Package and its Symbolize function: The definition is not attached to Subscript, but to pseudo-Symbol f1:

5

If I understand it correctly, the gist of your question is going from HoldComplete[x, y, z, up] to HoldComplete[1, y^2, z, up] assuming the following definitions: _[___, up, ___] ^= "UpValueEvaluated" x = 1 f[x_] := x^2 That is, evaluate everything inside the HoldComplete except the UpValue. I managed to do this using the following construction: ...

5

To answer my own question and further illustrate the kind of operation I am describing, here is a method using Set itself: SetAttributes[f, HoldAllComplete] f[args___] := Module[{h}, h[args] = 1; Level[DownValues@h, {4}, HoldComplete] ] f[own, down[1], sub[1][2], N[n], up] HoldComplete["OwnValue", "DownValue", "SubValue", 3.14, up]

5

The explanation can be found in the doc to BeginPackage and Begin BeginPackage["context"] makes context and Systemthe only active contexts. Begin["context"] resets the current context. Therefore, at the moment you first mention the symbol test it is not created it in the Global context. This can be seen from you (simplified) example ...

4

An example I use from time to time is to "prettify" output. Suppose you have a not so huge matrix, e.g. aa = Array[Subscript[a, #1, #2] &, {3, 3}] which prints with commas between the Indexes. Sometimes, you don't want them and you can replace them with InvisibleComma. To do this, I use the following: runocommaindex={Subscript[a_, b___, x_, y_, ...

3

You could introduce your own notation. Something like the following. SetAttributes[qualifed, HoldAll] Needs["Notation"] Notation[ParsedBoxWrapper[ RowBox[{"u_", "∘", "v_"}]] \[DoubleLongLeftRightArrow] ParsedBoxWrapper[RowBox[{"qualifed", "[", RowBox[{"u_", ",", "v_"}], "]"}]]] TagSetDelayed[qualifed, qualifed[u_, v_] = val_, TagSet[u, qualifed[u, ...

3

You should export the test symbol, ie define it between BeginPackage["TestPackage"] and Begin["Private`"], for example by giving a usage to test. test::usage = "test[name] returns ..." (*or just*) test; for your definition, as said already in comments, I would use test[name] ^= "hello"; My answer to this post contains many application of UpValues, it ...

2

An answer usng Mr.Wizards's suggestion This is an answer to the second question, with assidnments to Derivative[__][ftest][__], as suggested by Mr.Wizard in the comments: ftest2 /: Times[ a_ , ftest2[b_, c_] ] := ftest2[a b, a c]; ftest2 /: Plus[ ftest2[a1_, b1_], ftest2[a2_, b2_] ] := ftest2[a1+a2, b1+b2]; Derivative[1,0][ftest2][_,_] = ftest2[1,0]; ...

2

Here is my latest attempt at access functions ("getters") to emulate 2D data frames by overloading Part with UpValues on symbol DF. I would appreciate any improvements in efficiency (see test below) and generalization to handle more general headers like dates, which currently are not handled. This version uses Select as matching mechanism. Currently the ...

Only top voted, non community-wiki answers of a minimum length are eligible