Message Boards Message Boards

ChatGPT can presents Mathematica Object Oriented Codes still unseen

Posted 1 year ago

ChatGPT, AI language model makes a conversation, not only chat but also presents programming codes. It already knows Mathematica, and can produce the codes, however, checking the coding, we will soon understand the produced code is not a real code, saying, is the pseudo code.

So, produced pseudo codes have been meaningless? No, ChatGPT ability is fantastic one, and we should not abort without some considering to use.

I'd like to present some useful examples of the ChatGPT pseudocode, and working codes modified from the pseudo codes. AI can automatically produced, and plentiful, pseudo codes bunch as the samples. These characteristics may not be abandoned even if for Mathematica usage.

Pseudo sample 1

<< Developer`
Person::usage = 
  "Person[name_, age_] represents a person with the specified name \
and age.";
Person[name_, age_] := 
 Module[{}, Person[name, age] = Inherit[{name, age}, Person]]
Person /: Greet[Person[name_]] := 
 Module[{}, "Hello, my name is " <> name]
Person /: Birthday[Person[name_, age_]] := 
 Module[{}, Person[name, age + 1]]
p1 = Person["Alice", 25]
Greet[p1]
p2 = Birthday[p1]
p2

Working sample 1

Person[name_, age_] := Module[{myage = age},
  Greet[name] := "Hello, my name is " <> name;
  Birthday[name] := {name, age + 1}
  ]
Person["Alice", 25]
Greet["Alice"]
Hello, my name is Alice
Birthday["Alice"]
{"Alice", 26}

Pseudo sample 2

<< Developer`

Rectangle::usage = 
  "Rectangle[width_, height_] represents a rectangle with the \
specified width and height.";
Rectangle[width_, height_] := 
 Module[{}, 
  Rectangle[width, height] = Inherit[{width, height}, Rectangle]]
Rectangle /: Area[Rectangle[width_, height_]] := width height
Rectangle /: Perimeter[Rectangle[width_, height_]] := 
 2 (width + height)
Rectangle /: Draw[Rectangle[width_, height_], opts___] := 
 Module[{}, 
  Rectangle[width, height] = 
   Graphics[{Rectangle[{0, 0}, {width, height}]}, opts]]

r1 = Rectangle[10, 20]
Area[r1]
Perimeter[r1]
Draw[r1, Axes -> True]

Working sample 2

rectangle[name_ {width_, height_}] := 
 Module[{myw = width, myh = height},
  area[name] := myw*myh;
  perimeter[name] := 2 (myw + myh)
  ]

rectangle[r1 {10, 20}]
area[r1]
200
perimeter[r1]
60

Pseudo sample 3

<< Developer`
Employee::usage = 
  "Employee[name_, salary_] represents an employee with the specified \
name and salary.";
Employee[name_, salary_] := 
 Module[{}, Employee[name, salary] = Inherit[{name, salary}, Employee]]
Employee /: Raise[Employee[name_, salary_], amount_] := 
 Module[{}, Employee[name, salary + amount]]
Employee /: Paycheck[Employee[name_, salary_]] := 
 Module[{}, 
  "Employee " <> name <> " was paid $" <> ToString[salary] <> "."]

e1 = Employee["Bob", 50000]
e2 = Raise[e1, 1000]
Paycheck[e2]

Working sample 3

Employee[name_, salary_] := Module[{mySalary = salary},
  raise[name, amount_] := mySalary += amount;
  paycheck[name] := 
   "Employee " <> name <> " was paid " <> ToString[mySalary] <> "."
  ]
Employee["Bob", 50000]
raise["Bob", 1000]
51000
paycheck["Bob"]
"Employee Bob was paid 51000."

Enjoy, Mathematica OOP.

3 Replies

Stephen Wolfram just published a new article:

Wolfram|Alpha as the Way to Bring Computational Knowledge Superpowers to ChatGPT

https://writings.stephenwolfram.com/2023/01/wolframalpha-as-the-way-to-bring-computational-knowledge-superpowers-to-chatgpt

We started a new dedicated discussion here:

Stephen Wolfram on ChatGPT, Wolfram|Alpha & Computational Knowledge
https://community.wolfram.com/groups/-/m/t/2763581

POSTED BY: Moderation Team

Hello Sam !

It is easy task. First, you entered to ChatGPT page, and enter the following message as, "Show me an example of Mathematica Object Oriented Program code." ChatGPT will reply like following result. "Here is an example of an object-oriented program in Mathematica that defines a simple class called "Circle" with a single method called "area". This method calculates ..."

Have fun

Interesting! If I understand you correctly that Pseudo samples were produced by ChatGPT, then what were the prompts / queries you typed in the ChatGPT to get that Pseudo samples? Also why do they contain << Developer` line -- did ChatGPT put it there?

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

Group Abstract Group Abstract