How do I get CAS to evaluate a Python-in-CAS function? (Solved)
12-12-2019, 11:33 PM (This post was last modified: 12-13-2019 09:21 PM by StephenG1CMZ.)
Post: #1
 StephenG1CMZ Senior Member Posts: 852 Joined: May 2015
How do I get CAS to evaluate a Python-in-CAS function? (Solved)
How do I get this function to evaluate...
All I get is the name of the function returned
(The Python string syntax may need correcting too

Code:
 CAS file:  #cas HAMMINGCAS():= BEGIN def hammingdistance(s1, s2) -> int:     """Return the Hamming distance between equal-length sequences."""     if len(s1) != len(s2):         raise ValueError("Undefined for sequences of unequal length.")     count = 0     for ii in s1      el1 = s1(ii)      el2 = s2(ii)      if el1 != el2:       count = count+1     return count     """return sum(el1 != el2 for el1, el2 in zip(s1, s2)) """ END; #end PPL file:  EXPORT TRY()  BEGIN   LOCAL ST1:="AB";   LOCAL ST2:="AB";   RETURN CAS("eval(hammingdist(ST1,ST2))");  END; EXPORT HAMMING() BEGIN   //def hamming_distance(s1, s2) -> int:   // """Return the Hamming distance between equal-length sequences."""   //  if len(s1) != len(s2):   //      raise ValueError("Undefined for sequences of unequal length.")   //  return sum(el1 != el2 for el1, el2 in zip(s1, s2)) END;

Stephen Lewkowicz (G1CMZ)
ANDROID HP PRIME APP NO LONGER WORKS OFFLINE?
12-13-2019, 06:18 AM
Post: #2
 parisse Senior Member Posts: 1,135 Joined: Dec 2013
RE: How do I get CAS to evaluate a Python-in-CAS function?
Remove
HAMMINGCAS():=
BEGIN
and
END;
Then check your function from the shell.
Your loop is incorrect, it should be for ii in range(size(s1)): and you should write s1[ii] and s2[ii] (with 0 starting index, run index:=0 if required)
BTW, this is called hamdist in Xcas.
12-13-2019, 03:48 PM (This post was last modified: 12-13-2019 04:06 PM by StephenG1CMZ.)
Post: #3
 StephenG1CMZ Senior Member Posts: 852 Joined: May 2015
RE: How do I get CAS to evaluate a Python-in-CAS function?
The function works fine in CAS and PPL shell, and now from PPL

My error was naming the function hamming distance but calling hammingdist.
Fixing that it evaluates as expected.

Code:
 CAS: #cas def hammingdist(s1, s2) -> int:     """Return the Hamming distance between equal-length sequences."""     if len(s1) != len(s2):         raise ValueError("Undefined for sequences of unequal length.")     count = 0     for ii in range(size(s1)):      if s1[ii] != s2[ii]:       count = count+1     return count     """return sum(el1 != el2 for el1, el2 in zip(s1, s2)) """ PPL:  EXPORT TRY()  BEGIN   LOCAL ST1:="AB";   LOCAL ST2:="AB";   PRINT( CAS("(hammingdist(ST1,ST2))") );    PRINT(CAS("sum(N^2,N,1,5)")); //works   RETURN CAS("(hammingdist(ST1,ST2))");  END;

Stephen Lewkowicz (G1CMZ)
ANDROID HP PRIME APP NO LONGER WORKS OFFLINE?
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)