Tag Info

New answers tagged

5

Thanks to the link in the comment, I found a partial answer with Language`ExtendedDefinition. For instance: Language`ExtendedDefinition[Log] Language`DefinitionList[Log->{ OwnValues->{}, SubValues->{}, UpValues->{}, DownValues->{}, NValues->{}, ...


3

I would use Cases with a condition and SymbolName. Cases[step[2], (symbol_ -> _) /; StringMatchQ[SymbolName[symbol], "k*p"]] Another solution: step[2] /. (symbol_ -> value_) /; Not@StringMatchQ[SymbolName[symbol], "k*p"] :> Nothing This solution makes use of Nothing which is new in Mathematica version 10.2. Nothing could be replaced by ...


2

If you want to use a pattern, you could use Cases[step[2], Alternatives@@HoldPattern/@Thread[ToExpression[Names["Global`k*p"]]->_Real]] (* {k1p -> 1.5396, k2p -> 1.91751} *) Technically this does not convert anything to strings, it just looks up all the user-defined names that match "k*p". It's not really elegant though. Alternatively you can ...


4

I don't think you can avoid using strings, but that doesn't mean the output will contain strings. Pick[step[2], StringMatchQ[ToString /@ step[2][[All, 1]], "k*p"]] {k1p -> 1.09503, k2p -> 1.32185}


3

Here is a more complete example. ClearAll[fun, $opsPatt]; $opsPatt = Alternatives @@ {Minus, PreDecrement, Decrement, PreIncrement, Increment, Greater, Less , GreaterEqual, LessEqual, Equal, Unequal, Mod, Divide, Times, Subtract, Plus, BitShiftLeft, BitShiftRight, BitAnd, BitXor, BitOr, And, Or, Not}; SetAttributes[fun, HoldAll]; ...


0

This is a complete example. ClearAll[fun, $opsPatt]; $opsPatt = Alternatives @@ {Less, Greater, LessEqual, GreaterEqual, Equal, Unequal}; SetAttributes[fun, HoldAll]; fun[code_] := ReleaseHold[ Hold[code] //. { (head : $opsPatt)[x_, y_] :> COperator[head, {x, y}], HoldPattern[Set[lhs_, rhs_]] :> CAssign[lhs, rhs], ...


8

Indeed confusing, but can be explained. Please read here first on how Mathematica searches contexts for symbols. In short, $Context tells it where to create symbols. It's for creation, not for lookup. $ContextPath tells it where to look for symbols but doesn't affect symbol creation. If the symbol is not found in any of the $ContextPath contexts, ...


14

The definitions aren't being lost, they're being shadowed, as described in the tutorial on contexts. Mathematica doesn't warn you about this because it only warns when there is shadowing between contexts that are listed in the $ContextPath. Since Begin only changes $Context and not $ContextPath, you don't get a warning when the symbol that causes shadowing ...



Top 50 recent answers are included