Friday 18 July 2014

Using GCC Compiler on Windows

Being a student of an Indian college I got to learn C and C++ in starting of college using old and ancient TC compiler(on windows) which is not as per current standards that is C11 standards, as the wikipedia page of C programming language says that current standard of C compiler is defined by C11. When I came across experienced programmers they opened my eyes by telling same fact; so I decided to make my hand smooth in gcc and g++ compilers too which are as per C11 standards (have a look at list of C/C++ compilers). In Linux using these compilers is not at all a pain but recently I came across a situation when I have to run few C programs on Windows and I wanted to use only C11 standard compiler.

When I installed gcc on windows, realized that it could not be easy for newbies; so here presenting few steps to be followed to install and configure gcc and g++ compilers on Windows:

  1. Download and Install MinGW from sourceforge. It is a Minimalist GNU for Windows; basically it allows us to run gcc(GNU Compiler Collection) and GNU builtins on windows.
  2. After installion in the end it will ask to add some packages, we need at least GNU C++ compiler. Mark it for installation and other packages if you need.

  3. Click on installion menu above ---> Apply changes ---> Apply. While installing it will look something like below:
  4. Finally after installion it will look something like below image with slowing installed version. Close this window
  5. Set its bin path(mostly it is C:\MinGW\bin) to environment variable. For this follow following steps: 
    • Right click on my computer ---> Properties ---> Advanced ---> Environment Variables ---> system variables search for path variable ---> edit path ---> put a semicolom(;) at end of current path and append required path.
Now to run your C programs:
Open command prompt
Move to the folder where your .c or .cpp files reaside which you want to run. On command prompt write:
> gcc <filename>.c -o <filename>.exe  and hit enter
> <filename>.exe hit enter;
you will see result on command prompt.

That's it, hopefully this would be helpful for you, in case of any problem you are welcome to put them in comments.




Thursday 13 February 2014

Space Complexity

#What

It is just the amount of memory used by a program; the amount of computer memory that is main memory required by the algorithm to complete its execution with respect to the input size.

#Why

In todays world space is no more a hurdle what matters a lot is only time. So does one need to calculate space complexity?
Yes! If a program takes a lot of time, you can still run it, and just wait longer for the result. However if a program takes a lot of memory, you may not be able to run it at all, so this is an important parameter to understand.





 #How

Space Complexity(s(P)) of an algorithm is total space taken by the algorithm to complete its execution with respect to the input size. It includes both Constant space and Auxiliary space.

S(P) = Constant space + Auxiliary space

Constant space is the one which is fixed for that algorithm; generally equals to space used by input and local variables.
Auxiliary Space is the extra/temporary space used by an algorithm.




# EXAMPLE

 We assume that space required for one variable is one unit for simplicity.

1) def calculate(a,b,c):
     return a+b/c

S(p) = 1 + 1 + 1 = 3 ==> O(1)


2) def sum(a, n):
    s = 0
    for i in range(n):
        s += a[i]
    return s

S(p) = (n*1 + 1 + 1) + 1 ==> O(n) and auxiliary S(p) = O(1)




3) def Rsum(a,n):
    if n <= 0:
        return 0   
    return a[-1] + Rsum(a[:-2], n-1)



(# of stack frames)*(space per stack frame)

When the function calls itself there are 3 things that are stored in the stack.
The array pointer a,
The size of the array, n and
The value at the last index a[n].

So space per stack frame equals to {sizeof(a)+sizeof(n)+sizeof(a[n])}



This function calls itself till n becomes 0
so  # of stack frames = n (from n-1 to 0)

Therefor the amount of space required to store execute is
n times the space required to store above 3values once,

i.e., (n)*{sizeof(a)+sizeof(n)+sizeof(a[n])}.

Since the value in the braces remain a constant for a particular i/p,

we say that the space complexity of this is directly proportional to n.
i.e auxilary S(p) = O(n)

Saturday 5 October 2013

Mutuable Immutable Objects/Classes

Immutable objects are those whose contents/field values can't be change after their creation/instantiation, their values remain same up till  they resides in memory. They don't have any setter method, but do have getter methods to access them.
Mutable objects in contrast are those whose fields can be change, they have both setter and getter methods.


Eg of immutable classes in Java : all wrapper classes String, Integer, Byte, Short, Long, Character, Decimal, Float, BigDecimal, BigInteger.

Immutable objects at good parts:
  • Since they don't change their values/state, programmers often use them in multi-threading programs, we can share them with any number of programs without fear of any inconsistency problem. In other words whenever  sharing comes immutable objects play vital role as they are always consistent.They eliminate the code to make mutable objects safe, useful in concurrent programming.
  • They cannot be corrupted by thread interruptions, automatically thread safe.
  • No need of making defensive copies when returning or passing objects to other functions. 
  • String Pooling concept is safe because String objects are immutable.

At bad parts:
  • They can't be modified, thus deferentially a big hurdle when we need to change values of our instances(eg in gaming programs).
  • Always a new object is created, memory and thus performance/efficiency issues can occur.
  • Our perception of the real world is inevitably based on mutable objects. When you fill up your car with fuel at the gas station, you perceive it as the same object all along (i.e. its identity is maintained while its state is changing) - not as if the old car with an empty tank got replaced with consecutive new car instances having their tank gradually more and more full. So whenever we are modeling some real-world domain in a program, it is usually more straightforward and easier to implement the domain model using mutable objects to represent real-world entities.  

How to create Immutable classes(in Java):
Very first the variables/fields in class must be private.
Make class final so that methods inside class can't be overridden.
Set the value of the properties using constructor only and don't use any setter method, otherwise it be no more immutable.
Protect mutable fields in your class(if any):
  •    Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.
  • Don't provide methods that modify the mutable objects inside class, i.e,  don't forget overload/override the methods of mutable class (whose reference you are using), which can alter values of existing object.

Refrences:
http://programmers.stackexchange.com/questions/151733/if-immutable-objects-are-good-why-do-people-keep-creating-mutable-objectshttp://www.javaranch.com/journal/2003/04/immutable.htm
--
Thanks

Sunday 8 September 2013

Introduction to Data Science(Ws)



What

Data Science refers to an emerging area of work concerned with the collection, preparation, analysis, visualization, management and preservation of large collection of information.
                                                              -- Jeffrey Stanton
                                               Syracuse University School of Information Studies

From wikipedia Data science incorporates varying elements and builds on techniques and theories from many fields, including math, statistics, data engineering, pattern recognition and learning, advanced computing, visualization, uncertainty modelling, data warehousing, and high performance computing with the goal of extracting meaning from data, finding relationships in data and creating data products.


Why

In the 19th century or so there was a major change in the nature of solving problems, apart from becoming more abstract, the primary focus shifted from calculations or following procedures to analysing relationships. The change in emphasis wasn't arbitary, it came about through the complexity of the world. Data Science emphasis on finding unknown relationships among data.

Meanwhile approximately at the same time Internet also come in pace and as the Computer Science era changed from desktop application to web applications, and with more technology enhancement, now *huge*  amount of data is available every where, what needed is only to work on this data! All truths are easy to understand once they are discovered; the point is to discover them.

Lots and lots and lots of data, lots and lots and lots of people, lots and lots and lots of places, constant change, ever increasing data. Data Science is the new reality. Data as programmable resource, versioning, filtering, aggregating, extending, automating, analysing and communicating data becomes a need, not only for business perspective also for human welfare[2].
Data is ever growing, never at rest!



Where

Wherever is data, Data Science is followed. No matter data is small or in large scale, data science is used; For a new start-up with small data to big organizations with huge data, whenever one need to understand, process, extract value from it Data Science is applied.

What reviewed

Following are the three areas of data geeks:



                                                                                       -- Drew Conway

Hacking Skills refers to the programming knowledge and skills.
Math and Statistics refer to the traditional  research, the way one handles data. Math is also described as the science of patterns, thus here applied for pattern of data.
Substantive Expertise refers the basic knowledge about the data itself.


Data Scientist

Three types of Known and Unknowns:
  1. There are  Known Knowns, these are things we know that we know.
  2. There are  Known Unknowns,  things we know that we don't know.
  3. There are  Unknown Unknowns, there are things we don't know we don't know.

Data Scientists is the one who discovers the unknowns and make them known.  He need to find nuggets of truth in data and then explain it to the business leaders.






How it works

Though its never ending knowledge to understand how Data Science is applied on data, because every different set of data may require different technique/algorithm to analyse and interpret it. But still there are under mentioned tasks as a usual procedure followed for data analysis and communication.

Types of tasks in Data Science :
  1. Preparing to run a model: refers to gathering, cleaning, integrating, restructuring, transforming, loading, filtering, deleting, combining, merging, verifying etc.
  2. Running the model.
  3. Communicating the result: means visualization and interpreting final result.







--References:
  1. https://www.coursera.org/course/datasci
  2. http://blog.revolutionanalytics.com/2013/03/what-does-a-data-scientist-do.html