CodeGeneration Enhancement - sympy/sympy GitHub Wiki

Me the person

Name: Satya Prakash Dwibedi
Email: [email protected]
University: Biju Patnaik University of Technology
GitHub username: SatyaPrakashDwibedi
Phone no. :- +917751844623

Short Bio

I am currently pursuing the bachelor of technology in Information Technology. I love to make systems work as fast as possible and as efficiently as possible and coding them in the different languages with different efficiency and run time and make them fight in my mind about which one is more efficient and how the other one can be is fun.

Proposal

Abstract

Sympy a computer algebra system written in python has utility.codegen as it's submodule; which generates equivalent routines in supported languages from a given Sympy expression. My proposal aims to increase the list of supported languages in utility.codegen module and to do some changes in the previously existing code base in codegen.py which will increase the efficiency of routines generated in C.

Deliverables

For my project I plan on implementing the following:

  • Generating Javascript equivalent routine from given sympy expression
      >>>[(js_name,js_code)]=codegen(("test",x**y),"Js",empty=False)
      function test(x,y)
      {
            var test_result=Math.pow(x,y);
            return test_result;
      }

As this is not implemented in codegen module I would like to implement this.

>>>[(lisp_name,lisp_code)]=codegen(("test",x**2),"lisp",empty=False)
      (defun test (x) (* x x))

Implementation
Implementing Javascript and enhancing C code will take comparatively small time as I have experience of using them during my sem paper.
Javascript code printer has already been implemented inside printing module. So what I have to do is to add a class
JsCodeGenerator inside utilities.codegen as subclass from CodeGen, as it's functioning is very similar to C/FORTRAN, I won't have to override routine function and it supports only single return value so defining function is comparatively easy and here I won't have to worry about printing correct data types because in Javascript data types are not specified during declaration and then I will add test cases inside test_codegen.py.After successful testing I will submit a PR.
Reg. C code enhancement what I have to do is to perform a type check off which domain symbols belong to. I have no objection to working on this prior to GSOC because soon I finish the project I will focus on LISP code generation as I am new to this language it will require comparatively more time.
Reg. LISP code generation first I have to work on to implement lisp code printer inside printing module where I will have to look through its inbuilt functions and add a dictionary mapping from known sympy functions to LISP functions ,a list of reserved keywords, add necessary LISP macros if required and as in LISP all expressions are prefixed I will implement infix to prefix conversion algorithm which will return correct LISP expression then I will add corresponding printing methods to print LISP expressions, loops, inbuilt functions .After successful implementation of lisp code printer I will move to Lisp Code generation where I will add a class Lisp code generator as base class from CodeGen, will override routine method as it supports multiple return values and then add methods inside it to print function opening and closing and a call to lisp code printer which will return corresponding LISP expression.Then add test cases inside test_codegen.py.

Why did I chose it

I personally find it really confusing when I switch between languages as different purposes. So I always thought if learning one language can serve the purpose then it would be awesome and sympy.utility.codegen package have ability to accomplish it. By enhancing it i.e adding more and more functionalities to it means increasing the super power of the package which will increase its user count and students like me will be greatly benefited from it as we won't have to run from languages to languages and wasting time to remember the syntax and get acquainted with it. As javascript is widely used in dynamic web programming I think implementing this will give Sympy users a great deal of benefit to implement complex mathematical operations and functions inside web pages i.e java script implementation with minimum utility of manpower and time.
LISP as the name suggests Lots of Irritating Superfluous Parentheses is confusing to write or program with though it is vastly used in the field of Artificial Intelligence and implementing this will give Sympy users as well as python users an upper hand and I think it would be pretty cool and impressive to generate such complex irritating code from a beautiful simple code just through the use of Sympy.
C routines printed in codegen from sympy expressions involving symbols in complex domain has not been implemented and data type double is printed for all; even when a variable can work with int data type which takes less memory in compairison to double. I think adding datatypes specific to symbols will take less memory and will be efficient.

How do I fit in

Me as the programmer

  • Platform and Editor
    I work on windows platform and prefer IDLE(Python GUI) as primary editor for python and sometimes Sublimetext3. I prefer IDLE because it gives python type look and I prefer it over other interpreters because it's light and doesn't take much CPU space.
  • Programming Experience
    I started coding in C in my first year, then took C++ in the second year and I love C because it has the powerful thing called pointers using which we can manipulate hardware and memory blocks.Then in my third year I just tried to learn this new language called Python but after writing 3-4 lines of code I fell in love with it because as it's said: "SIMPLICITY IS BEAUTY". I also have experience in SQL, Java and now I have Javascript and HTML in my Sem. so I am almost at the end of the completion of these languages.
    Projects I have done include:
    1. https://github.com/SatyaPrakashDwibedi/File_Reader: - Reads a text file choose by user; designed using Python
    2. https://github.com/SatyaPrakashDwibedi/TEXTEDITOR: - A simple Text Editor
    3. https://github.com/SatyaPrakashDwibedi/Proposal_card: - A proposal card to ask a girl out.
    I love open source coding as it helps to know other developers and improve one’s coding abilities as well as innovating ideas to solve problems and lets students and developers like us to manipulate codes according to our requirement.
  • Flavours of sympy
    Consider the following code snippet
>>> x,y=symbols('x y',integer=True)
>>> from sympy.utilities.codegen import codegen
>>> [(c,d),(e,f)]=codegen(("f",x**y),"C",header=False,empty=True)
>>> print d
#include "f.h"
#include <math.h>

double f(int x, int y) {

   double f_result;
   f_result = pow(x, y);
   return f_result;

}

this is cool because instead of 6 lines in C, I just had to write 4 line in python using Sympy package. This is just a glimpse of how marvelously it can help to solve complicated mathematical statements. This is not just one and It can solve complicated mathematical statements

>>> i=symbols('i')
>>> expr=Sum(sin(cos(sin(i)))**2,(i,0,9))
>>> expr.doit()
sin(cos(sin(8)))**2 + sin(cos(sin(5)))**2 + sin(cos(sin(2)))**2 + sin(cos(sin(1)
))**2 + sin(cos(sin(4)))**2 + sin(cos(sin(7)))**2 + sin(cos(sin(9)))**2 + sin(co
s(sin(6)))**2 + sin(cos(sin(3)))**2 + sin(1)**2

So, basically it's awsome.

  • Qualifications I do have to implement the idea
    I started working on sympy since march 2017 within this span of time I have already gone through the sympy.utility.codegen source code as well as some of sympy.printing source codes, understood the flow of execution and contributed to
  1. https://github.com/sympy/sympy/pull/12298 (merged)
  2. https://github.com/sympy/sympy/pull/12433 (to be merged) Opened some issues which are under discussion and will be implemented once got the clear cut idea about implementation:
  3. https://github.com/sympy/sympy/issues/12423 (solved)
  4. https://github.com/sympy/sympy/issues/12453 (Under discussion)
  5. https://github.com/sympy/sympy/issues/12356 (found duplicate) also have thorough knowledge about how Git, Gitter, Mailing list works and also the code of conduct.
    I have good knowledge in python as I have done some projects using python so I am completely comfortable to write code in it.
    I have thorough knowledge reg. C and Javascript, it will be easy for me to detect the bugs while adding Javascript functionality and enhancing C code.
    Currently, I am going through http://www.tutorialspoint.com/lisp/ study material for LISP and https://www.youtube.com/playlist?list=PLD1CDF290815C750D, after completion, I can easily implement and find bugs in LISP code generator and LISP printing module while developing them.
    I would love to see lazy python programmers like me to be able to write routines in different languages even the most irritating ones(LISP) and rule in every field of computer science so I think It would be great if you give me a chance to work on this project and I promise to put my best into it.

Planning

Pre GSOC

I will contribute 4 hrs daily (as it is the semester exam time) to discuss reg. Idea and Implementation strategies with mentors and will start working on Enhancement of C code and solve more issues if possible.

During GSOC

During GSOC period I am willing to give minimum of 8hrs. daily. WEEK 1,2,3 :- FOR ADDING JAVASCRIPT
Start working on addition of Java script codegeneration class inside utilities.codegen and add methods inside it. After addition of methods and class I will move to test_codegen.py and add test cases. After successful testing a PR will be issued.
WEEK 4: - FOR LEARNING LISP THOROUGHLY
In this week I would like to take some time to get deep into LISP and will work on implementing algorithm to convert infix to prefix conversion.
WEEK 5, 6, 7, 8: - FOR ADDING LISP CODE PRINTER INSIDE PRINTING MODULE
I would like to contribute more time to addition of lisp code printer module here I will work on adding functions to print LISP expressions and addition of test cases inside tests.printing.lispcode and after successful testing a PR will be issued.
WEEK 9, 10, 11: - FOR ADDING LISP CODE GENERATION INSIDE UTILITIES.CODEGEN MODULE
In this week I would like to work on adding LISP code generation class inside CodeGen class, overriding routine method and addition of methods to print function structure.
WEEk 12: - BACK UP
I would like to keep this week as my back up in case any of the above takes more time and if project finishes early I will start working on adding PythonCodeGenerator inside utilities.codegen module.

Post GSOC

After successful submission of project I will work on optimizing codegen generated routines, solve issues; if any pops and aim to add more functionality to utility.codegen module as well as printing module as they are inter connected.

References:

  1. CodeGeneration Notes - https://github.com/sympy/sympy/wiki/Code-Generation-Notes
  2. LISP notes - https://www.tutorialspoint.com/lisp
  3. Infix to Prefix algorithm - http://scanftree.com/Data_Structure/infix-to-prefix
  4. LISP lectures - https://www.youtube.com/playlist?list=PLD1CDF290815C750D
  5. https://github.com/sympy/sympy/wiki/GSoC-2009-Application-Aaron-Meurer
⚠️ **GitHub.com Fallback** ⚠️