saad khalid wrote:
> Sorry! I hadn't seen that, that's great, thank you! The denominator is
> proving to be a bit tricky to deal with, mostly because I can't decide
> what I want to do with it. I did it though, but I'm not happy with it.
> I'm also an amatuer at regex(this is the first time I've used it, I
> figured it was about time I started learning now). But, I Think it
> should work? So, I made another method named sage_prodstring that does a
> similar job to sage_polystring. Here is the code for it:
> 
> |
>     defsage_prodstring(self):
>     """
>         If this Macaulay2 element is a Product, return a string
>         representation of this Product that is suitable for
>         evaluation in Python. Needed internally for using to_sage on
> objects of class Divide.
> 
>         EXAMPLES::
> 
> 
>         """
>     external_string =self.external_String()
>     prod_String =re.findall("new Power from
> \{(.+?),(.+?)\}",external_string)
>     final_Prod =""
>     fori inrange(0,len(prod_String)):
>         final_Prod +="("+prod_String[i][0]+")"+'^'+prod_String[i][1]+"*"
>     final_Prod =final_Prod[:-1]
>     returnfinal_Prod
> |

Side note:  Never use += on a string in a loop.  Here it's even worse,
since you are concatenating strings on the right hand side as well.

Hint:  "*".join(["alpha","beta","gamma"]) gives 'alpha*beta*gamma' for
example.

In your case the list to join is something like

  [ "(%s)^%s" % (f[0], f[1]) for f in prod_String ]

so you could replace the five last lines by

   return "*".join( [ "(%s)^%s" % (f[0], f[1]) for f in prod_String ] )


(Haven't tested that, your EXAMPLES or TESTS should do though...)


-leif


> I don't really know what to put in the Examples part. If you have any
> recommendations, I'd love to know. Also, I feel like this implementation
> is inneficient, since it first runs external_string, which is a Sage
> method that parses Macaulay's ascii art into a str, and then my method
> parses that str into something that you could actually use as python
> code. I feel like, ideally, I wouldn't need to run external_string, and
> could directly convert the ascii output from macaulay directly into the
> python code output I wanted, but i couldn't figure out exactly how to do
> that... I hope this is okay. I don't think i can test it yet until I
> fill in the spot for the Example though.
> 
> I then used this method in the to_sage() function for the denominator,
> as seen here:
> 
> |
>             elifcls_str =="Divide":
>                 div_Numerator =self.numerator()
>                 div_Denominator =self.denominator()
>                 div_Numerator =div_Numerator.sage_polystring()
>         div_Denominator =div_Denominator.sage_prodstring()
>                 sage_Div ="("+ div_Numerator ")"+"/"+"("+div_Denominator
> +")"
>                 returnsage_Div
> |
> 
> How does it look? Thanks for all the help thus far.


-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to