Tag Info

Hot answers tagged

26

This is a simplification of your solution: Language`ExtendedFullDefinition[new] = Language`ExtendedFullDefinition[old] /. HoldPattern[old] :> new I believe Language`ExtendedFullDefinition 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 ...


18

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]} ...


13

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[__], ...


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


10

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


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

Warning: Modifying a built-in function is not advised As @m_goldberg already stated, Lookup has Attributes HoldAllComplete, so a workaround will be to remove this Attribute: Edit: As per m_goldberg's recommendation attr = Attributes[Lookup]; Attributes[Lookup] = {}; Now t1 = TempHead[a -> 1, b -> 2, c -> 3]; t2 = TempHead[c -> 3, d -> 4, ...


7

[Added: Virtually the same issue came up on StackOverflow a few years ago: Why do I have to evaluate this twice?] I would not consider it a bug if the manipulation of System` variables do no go the way you want. It turns out that the definition of TensorRank is not loaded until it is first evaluated. That initialization process resets the attributes of ...


7

Another solution for this weird exercise is to make a combination from using $Pre and defining a new plus function. You use $Pre to replace every occurrence of Plus by your own definition which only act special at the input plus[2,2] and calls the normal Plus otherwise: SetAttributes[plus, Attributes[Plus]]; Unprotect[plus]; plus[2, 2] = 5; plus[args___] := ...


7

You don't need to use TagSetDelayed for the definition of the derivative because Derivative doesn't have attribute Protected. I'll extend add the derivative definition to arbitrary order n: ClearAll[ln]; Derivative[n_, 0][ln][x_, a_] := Derivative[n][Log][x] ln[x_, a_?NumericQ] := Piecewise[{{Log[x], Re[a] > 0}, {-Log[1/x], True}}] ln[x, -1/2] ...


6

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


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] // ...


6

The explanation can be found in the doc to BeginPackage and Begin BeginPackage["context`"] makes context` and System`the 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 ...


6

How about this ;-) \!\(\*InterpretationBox[2,3]\)+2


6

Lookup has the attribute HoldAllComplete, which means the kernel evaluator will not see its arguments and, therefore, will not look at its up-values.


5

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


5

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_, ...


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

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

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

OptionValue and OptionsPattern[] are magical constructs, which work by certain macro-like trick at run-time. So, I am not surprised that OptionValue did not work here. I would suggest to use it's long form: OptionValue[f, {opts}, optionName], and declare options as opts:OptionsPattern[], rather than just OptionsPattern[] - it will work then, and ...


4

You could do: Unprotect[Dot]; SetAttributes[Dot, HoldAll] Then tab1 /: tab1.column1 = 1 ; column1 = "x"; tab1.column1 returns 1 Is this what you want?


4

Ad 0. First of all, t in deq is not scoped so if you are assigning Tuples to t, the mess will occur. Ad 1. You don't need Hold. From the documentation of Tuples: Tuples[{list_1, list_2, ...}] generates a list of all possible tuples whose i-th element is from list_i. There is no list in deq so Equal is splitted. The proper syntax is: Tuples[{{deq}, ...


4

I believe the crux of your problem is, as you say, how to match n_NewHead. Let's look at the FullForm: FullForm[n_NewHead] Pattern[n, Blank[NewHead]] As you can see this is composed of Blank and Pattern, both of which are special heads with regard to pattern matching. You therefore need to wrap them in Verbatim to make a literal match: MatchQ[ ...


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

As @Mr.Wizard asked, "...it's time for an answer, don't you think?". I forgot about this question, but after year it occurred to me to formulate an answer following @LeonidShifin suggestions: First get some test data: testData = FinancialData["GE", "Jan. 1, 2000"][[1 ;; 10]]; dataFile = "/Users/jagra/Data.test.csv"; Export[dataFile, testData, "CSV"]; ...


3

Your problem has nothing to do with the function foo, but rather with the definition itself. Observe: a = {7, 8, 9}; a /: a[i_] := a[[i]] TagSetDelayed::tagnf: Tag a not found in {7,8,9}[i_]. >> Because heads evaluate first you cannot even use the attempted definition for a while a has a direct (OwnValue) assignment: ClearAll[a] a /: a[i_] := z[i] ...


3

Update In my original answer (which I have left below for entertainment value) I asserted that it would be difficult to make it work with upvalues using D, but Tom has reminded me of the NonConstants option. When the symbols listed as NonConstants are differentiated they remain as expressions with head D: D[a x, x, NonConstants -> {a}] (* a + x D[a, x, ...


3

Using information from the question you've linked: Derivative[1][X][_] = A Derivative[1][Y][_] = B D[X[x], x] D[X[x] + Y[x], x] D[X[x] Y[x], x] A A+B B X[x] + A Y[x] Nothing else is required since D[X[x], y] is from definition 0.



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