Group Abstract Group Abstract

Message Boards Message Boards

10
|
12.9K Views
|
23 Replies
|
25 Total Likes
View groups...
Share
Share this post:

[WSG23] Daily Study Group: Writing a Wolfram Language Function

Posted 2 years ago
POSTED BY: Daniel Robinson
23 Replies

Regarding Mapping of operators: How do I map the derivative operator onto a list of expressions?

For example, instead of doing

{D[-Sqrt[4 - x^2], x], D[Sqrt[4 - x^2], x]}

I'd like to have the list of expressions {-Sqrt[4 - x^2], Sqrt[4 - x^2]} and map the Derivative operator onto the list directly. But there is this pesky 'x' in the definition of the operator D[f,x]. How is that done?

POSTED BY: Carl Hahn
POSTED BY: Paul Erickson

Doh! Thanks Paul! I don't know why I did not think of the first option right away, but I didn't; and it does work. Exactly as it should; and exactly what I was looking for. Come think of it, it's a perfect little example of how the syntax of the Wolfram language works. So thanks for the tip.

Carl

POSTED BY: Carl Hahn

In the lecture, you show the full local names on a few occasions. How is that done? For instance x=1; y=3, Plot{ x, {x,0,y}; x might show as x=1; y=3, Plot{ x$12345, {x$12345,0,y}; x clearly showing the localize, temporary definition of the x inside the Plot... How do you switch between the two views of the cell?

POSTED BY: Paul Erickson
POSTED BY: Arben Kalziqi

No, mostly made up, it was in the live presentation, but I don't think shows in the notebook. I assume it a front end maybe cell type or something. The presenter switch back and forth but I didn't hear if he indicated how. I was thinking it was a plot, but not sure - just some expression that had a mixture of localized and global in the expression.

POSTED BY: Paul Erickson
POSTED BY: Arben Kalziqi

In the last session, I mentioned that Alternatives (a pattern object) and Or (a function) are not interchangeable. Here's an example to demonstrate how they differ.

Consider trying to extract all of the cases from a list that are either 2 or True. Using Alternatives (|) gives the correct result:

Cases[{1, 2, 3, True, 4}, 2 | True]
(* {2, True} *)

However, using Or (||) gives an incorrect result:

Cases[{1, 2, 3, True, 4}, 2 || True]
(* True *)

This is because Or immediately evaluates if any of its arguments resolve to True or False. In this case, 2||True evaluates to True, so Cases only picks out the items that match True.

POSTED BY: Daniel Robinson
POSTED BY: Daniel Robinson

Wow! Note that it needs 13.3 to evaluate PlotHighlighting -> None, If you delete that statement it works with 13.2

POSTED BY: Carl Hahn
POSTED BY: Hubert de Guise

Hi Hubert, your questions can be answered with overloading – something we will be covering in today's session (Extending Capability).

To solve (c), you can use OrderlessPatternSequence, and to solve (b), you can use Condition (/;). Both of these were covered in the first session (The Basics). To solve (a), you can use Message, which will be covered in the fourth session (Error Handling).

Here's some code to get you started:

ClearAll[getClass];

getClass::orderTooLow = "The order you have provided (`1`) is too low. It must be an integer greater than 2.";
getClass::badArgs = "Bad arguments. getClass takes exactly 2 arguments: an 'order' and a 'cycle'. The order must be an integer greater than 2 and the cycle must be a valid permutation cycle.";

(* Main definition: *)
getClass[
    OrderlessPatternSequence[
     order_?IntegerQ,
     cycle_?PermutationCyclesQ
     ]
    ] /; order > 2 := (* Your function *);

(* Order provided is too low: *)
getClass[
   OrderlessPatternSequence[
    order_?IntegerQ,
    cycle_?PermutationCyclesQ
    ]
   ] := (
   Message[getClass::orderTooLow, order];
   $Failed
   );

(* Arguments provided are either invalid, too numerous or too few: *)
getClass[badArgs___] := (
   Message[getClass::badArgs];
   $Failed
   );

You can add more definitions / error messages to handle more cases.

P.S. – you seem to have a lonely p in your function definition.

POSTED BY: Daniel Robinson
POSTED BY: Hubert de Guise

Any reference of how to order/place the functions in a mathematica packages , context etc, will be useful.

Hi Francisco, these workflows explain the details:

For an example, take a look at the ImageStyler.wl file (which is referenced in the fifth notebook, Useful Tips).

POSTED BY: Daniel Robinson
POSTED BY: Carl Hahn
POSTED BY: Daniel Robinson

Thanks! That's interesting. Just curious: Where in the vast warehouse of Documentation files would I find this little factoid?

Carl

POSTED BY: Carl Hahn

I just did a search myself in the documentation and couldn't find anything particularly helpful… (no tutorial that explains how to create your own operator forms, anyway).

However, in case it's of interest to you, I did find some code on StackExchange that claims to list all of the bulit-in functions that have operator forms:

file = FileNameJoin[{$InstallationDirectory, "SystemFiles", "Kernel", 
        "TextResources", $Language, "Usage.m"}];

usage = Import[file, "HeldExpressions"];

usage //
  Cases[
    HoldPattern[
      _[_[sym_Symbol, "usage"] = _String?(StringContainsQ["operator form"])]
    ] :> sym
  ]
POSTED BY: Daniel Robinson

Thanks for first lecture. I am interested in learning more about sequences. Your introduction is difficult for me to apply to a sequence of mathematical operations. I want to write a function to accept pullback metrics and compute the sequence as a parameter varies. Any guidance/suggestion would be appreciated. Larry

POSTED BY: Laurence Bloxham

Hi Laurence, can you give me some more details? It would be best if you could write down a simple example that includes the setup you want to start with and the result you want to end with. I may then be able to help with the steps in between.

POSTED BY: Daniel Robinson
Posted 2 years ago

Excellent first session!

POSTED BY: Gregory Lypny
POSTED BY: Jamie Peterson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard