Group Abstract Group Abstract

Message Boards Message Boards

1
|
4.7K Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Resolved: Given a symbol, how do I "strip" off the context prefix?

The following issue is now resolved -- with thanks going to @Eric Rimbey for pointing out my mis-use of the form Needs["context" -> "alias"] prevented my paclet symbols from being loaded into $Context. Attached is the working example where using Needs["context"] solves my issue.

The original question follows.


In my paclet, I have a function that extracts relevant symbols from an equation to return a sorted symbols list. But my implementation strategy fails when anyone calls my paclet function from a different namespace context. Is there a means of creating a function were I can add a blank sequence for the context? Or, is there a more generalized strategy for handing symbols between different contexts?

Implementation:

pSort[ M ] = 1;
pSort[ w[id_] ] := 100 + id;
pSort[ A[id_] ] := 200 + id; 

extractSymbols[ equation_ ] :=
      Block[{varsAndCosVars, deDupedAndAll},
       varsAndCosVars = (Variables @ equation) /. (Cos[vars_] :> Variables @ vars);
       deDupedAndAll = 
    DeleteCases[DeleteDuplicates[Flatten[varsAndCosVars]], t]];

Test:

testEq1 = A[2]*Cos[w[2]*t] + A[1]*Cos[w[1]*t] + M;
Sort[extractSymbols @ testEq1, pSort[#1] < pSort[#2] &]

Expected Result:

{M, w[1], w[2], A[1], A[2]}

Problem: While the above does work in the paclet context space, it fails when extractSymbols is invoked from another context where the Symbols in the equation are unique to caller's context space.

POSTED BY: Chase Turner
6 Replies

Does adding t as a argument work for you?

extractSymbols[ equation_ , t_] :=
      Block[{varsAndCosVars, deDupedAndAll},
       varsAndCosVars = (Variables @ equation) /. (Cos[vars_] :> Variables @ vars);
       deDupedAndAll = 
    DeleteCases[DeleteDuplicates[Flatten[varsAndCosVars]], t]];

If you want an "operator form," you could define it thus:

 extractSymbols[t_][ equation_ ] := ...

Then extractSymbols[t] @ equation.

POSTED BY: Michael Rogers

Thank you -- I had not previously appreciated how to use the operator form and now I know!

POSTED BY: Chase Turner
Posted 2 years ago
POSTED BY: Eric Rimbey

Bingo -- Thank you for helping me to see where I had gone wrong. Per your suggestion, all is now working as expected!

POSTED BY: Chase Turner
Posted 2 years ago

You should show us how exactly you set up your paclet (e.g. how you used BeginPackage, Begin, or whatever). But I suspect that the problem is to do with your symbols M, w, A, and t. You should declare those symbols in your package context. pSort and extraceSymbols should also be declared, but based on the way you asked the question I assume that you already declared those.

POSTED BY: Eric Rimbey

Please save "ContextA.wl" in the same folder as "CallsContextA.nb". Then, Open "ContextA.wl" and confirm the VerificationTests work as expected. Open "CallsContextA.nb" and observe VerificationTests will fail.

How should I write ContextA's functions so they work with Symbols {M, A, w, t} from any context -- without forcing ContextA to define them as Global? NOTE: Assuming a .nb has a "Needs["ContextA" -> "a"], I do not want there to be a solution where equations have to be written as follows:

a`A[2]*Cos[a`w[2]*t] + a`A[1]*Cos[a`w[1]*t] + a`M
POSTED BY: Chase Turner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard