NumPy Project Euler Problem 5

This entry is part 5 of 9 in the series NumPy Project Euler

Project Euler Problem 5is one of those problems that seem hard, but turn out to be trivial after you think about them. In this case it is very important to use the information given to you in the problem. This means using the fact that 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. We can therefore limit our search to multiples of 2520.

1. Create a divisors array

First we will create a divisors array. We only need to divide by the numbers 11 – 20.

divisors = numpy.arange(11, 21)

2. Check for 0 remainder

Second make sure that all the divisions of a number by the divisors produce a 0 remainder. We can check for that with the NumPy all function.

numpy.all((i % divisors) == 0)

3. Test the solution

Eventually we will get an answer. We need to check whether this is the correct answer. Use the assert_equal function from the numpy.testing module to confirm the result.

numpy.testing.assert_equal(numpy.zeros(19), i % numpy.arange(2, 21))

Below is the complete code solution.

import numpy
 
#2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
 
#What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
 
# 1. Create a divisors array
divisors = numpy.arange(11, 21)
 
for i in xrange(2520, 10 ** 9, 2520):
        # 2. Check for 0 remainder
        if numpy.all((i % divisors) == 0):
                print i
                # 3. Test the solution
                numpy.testing.assert_equal(numpy.zeros(19), i % numpy.arange(2, 21))
                break

 

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly. The NumPy Christmas Book Giveaway has officially ended. Winners will be contacted by the publisher. A new review of the book was added on Amazon.

Share
Posted in programming | Tagged , , | Leave a comment

Technical Career Navigator Review

In these hard economic times The Technical Career Navigator by Ray Weiss is the perfect book to help you find a job, keep your job and advance your career. The book is written for programmers, hardware engineers, technical managers and documentation writers. It consists of two alphabetically sorted lists of one to three page articles. Each article starts with one or more relevant quotes. The first list contains general tips for the intended audience. The second list contains job search tips.

Technical Navigator
Author Ray Weiss
ISBN-10 013148396X

I tried dividing the first list in some sublists for the purpose of this review.

Career

As expected the book contains a number of general career insights.

  • Be Noticed – do your work and make sure you’re noticed.
  • Consulting – tips on avoiding doing grunt work, setting your rates, growing your skillset and finding new projects.
  • Decisions – we make so many decisions in our working life, that we need to review where we are heading at least once a year.
  • Hairy Arm – leave an obvious flaw in your design or proposal. People want to have something to fix before accepting your idea.
  • I don’t Know – it’s hard to say that you don’t know something, but it is the best thing to do.
  • Lateral Thinking – a different way to solve problems.
  • Line Versus Service – work where the money is, line organizations.

Product Marketing

Surprisingly, the book also has advice on product marketing.

  • Betting On Instinct – most companies do extensive market analysis, however eventually decisions are made not based on numbers, but intuition.
  • By The You-Know-Whats – this one doesn’t require any explanation.

Technical Management

Technical management pitfalls are also discussed in the book.

  • Breaking Owsie Chains – owsie chains are bad. You need to find a way to break them.
  • Consent of the Governed – managers do not have a Divine Right to rule.
  • Credit Theory of Management – this theory compares management to credit banking.
  • Fear – you cannot innovate if you live in fear.
  • Gearing up – shifting from mediocrity to superiority.
  • Hiring – a list of hiring techniques is given.
  • It’s not What you do – do not expect other people to work in the same way or at the same level as you do.

Writing and Documentation

The author is a former technical editor with years of experience writing documentation.

  • Conceptual Maps – needed for documentation. I wonder if mind maps are meant here.
  • Learn to Write – writing can be learned. You need to read and practice a lot. There are two stages. First, aim for simplicity. In the last stage you try to perfect your style. For a technical writer content is more important than style.
  • Writing Memos, Letters – use a pyramidlike approach, put concepts at the top and details at the bottom. Use contractions and keep the text simple.

Programmer or Engineer

The writer seems to make a distinction between programmers and hardware engineers.

  • Design Reviews – reviews are needed to find errors early.
  • Engineering Notebook – you should have one, preferably a paper notebook.
  • Expecting Too Much – you don’t expect your code to work the first time you run it, do you? Why would it be different in real life?
  • Invest 30 minutes a Day – set time aside each day to keep current.
  • Pat Yourself on the Back – as you become more senior you will get less and less kudos from management. Know your worth.
  • Product Track Record – make sure you have some successes.

Job Search Tips

At the end of the book we find hints and observations about job hunting.

  • Catching a Horse – finding a job is easier, while you have one.
  • Contacting People – hints on contacting managers and recruiters.
  • Do It In Parallel – search in parallel fashion.

Technical Career Navigator contains many hidden gems. It’s a book you will be reading again and again. I give this book 5 stars out of 5.

Share
Posted in books | Leave a comment

NumPy Project Euler Problem 4

This entry is part 4 of 9 in the series NumPy Project Euler

Project Euler Problem 4 is a bit silly. It’s about palindromic numbers. I could not find an appropriate algorithm, so I just tried something out. It seems to work OK.

1. Create a 3-digit numbers array

We will create an array to hold 3-digit numbers from 100 to 999 using our favorite NumPy function arange. Check the first and last element of the array with the assert_equal function from the numpy.testing package.

a = numpy.arange(100, 1000)
numpy.testing.assert_equal(100, a[0])
numpy.testing.assert_equal(999, a[-1])

2. Create the products array

Now we will create an array to hold all the possible products of the elements of the 3-digits array with itself. We can accomplish this with the outer function. The resulting array needs to be flattened with ravel, to be able to easily iterate over it. Call the sort method on the array to make sure the array is properly sorted. After that we can do some sanity checks.

numbers = numpy.outer(a, a)
numbers = numpy.ravel(numbers)
numbers.sort()
numpy.testing.assert_equal(810000, len(numbers))
numpy.testing.assert_equal(10000, numbers[0])
numpy.testing.assert_equal(998001, numbers[-1])

3. Find the largest palindromic number

At this point I decided to continue with standard Python. We can probably do something fancy with NumPy, but it looked like reversing string representations of the numbers, and comparing them to the originals would be simpler.

for i in xrange(-1, -1 * len(numbers), -1):
   s = str(numbers[i])
 
   if s == s[::-1]:
      print s
      break

Below is the complete program.

import numpy
import numpy.testing
 
#A palindromic number reads the same both ways.
#The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
 
#Find the largest palindrome made from the product of two 3-digit numbers.
 
#1. Create  3-digits numbers array
a = numpy.arange(100, 1000)
numpy.testing.assert_equal(100, a[0])
numpy.testing.assert_equal(999, a[-1])
 
#2. Create products array
numbers = numpy.outer(a, a)
numbers = numpy.ravel(numbers)
numbers.sort()
numpy.testing.assert_equal(810000, len(numbers))
numpy.testing.assert_equal(10000, numbers[0])
numpy.testing.assert_equal(998001, numbers[-1])
 
#3. Find largest palindromic number
for i in xrange(-1, -1 * len(numbers), -1):
   s = str(numbers[i])
 
   if s == s[::-1]:
      print s
      break

Have a Go

Although I am pretty sure this is the right solution, it might be a good idea to check the result. Find out which two 3-digit numbers produce our palindromic number by modifying the code a bit. Try implementing the last step in a NumPy way.

 

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly. I would like to make a few announcements. First, I was recently interviewed by floss4science about the book. Second, a gentle reminder that the Christmas NumPy Book Giveaway is going to end this month.

Share
Posted in programming | Tagged , , , | Leave a comment

Refactoring Book Review

Refactoring is the process of rewriting software, without changing the way it functions, in order to improve its readability, testability or maintanability. A book has been written on the subject by Martin Fowler. Some of the chapters were written by other people.

“Refactoring: Improving the Design of Existing Code” is focused on OO programming (lots of Java examples) and Agile practices. It is setup as a catalog of refactoring techniques. Each page dedicated to a refactoring is clearly marked, so that you can easily look up a refactoring.

The book was written a decade ago. At that time IDE’s did not support advanced refactorings. Most of the methods described are fully automated in modern IDE’s. This means that you don’t have to follow the text that closely any more.

Refactoring Book
Author Martin Fowler, Kent Beck, John Brant and William Opdyke
ISBN-10 0201485672

1. Refactoring, a First Example

The first chapter starts with a simple example. Java code is used throughout the example as well as UML diagrams.

2. Principles in Refactoring

The Why and When of refactoring are discussed. Also we get instructions on what to tell our manager about refactoring. This seems a bit silly to me, since I have never had to explain refactoring to my managers.

3. Bad Smells in Code

“Code Smells” is a frequently used Agile phrase. A phrase I don’t care that much about. Code works or it doesn’t, it can be ugly or unreadable, but it doesn’t smell. The list of “smells” makes sense, however some of the names are downright confusing. For instance, would you be able to tell me what “Refused Bequest” means?

4. Building Tests

This chapter talks about JUnit at length. I am sure you are aware that there are many other unit testing frameworks for programming languages other than Java such as PyUnit. We are told that before you start refactoring, you need to have tests. I think it is more of a chicken/egg dilemma. Sometimes you need to refactor first in order to test. Unit tests and functional tests are mentioned. Integration tests, however are completely ignored. How would you know whether the performance and memory usage of your system remained the same? Clearly, this chapter was written by a software developer, and not by somebody who likes breaking applications, I mean testing applications.

5. Toward a Catalog of Refactorings

Chapter 5 describes the catalog of refactorings to follow. It is the catalog metada in a sense.

6. Composing methods

This chapter is the beginning of the catalog, which forms the “meat” of the book. I am just going to mention a few of the techniques listed in chapter 6.”Extract Method” is one of those refactorings I use on a daily basis. Sometimes things go wrong so we have to do the opposite refactoring “Inline Method”. The author starts using the term “temp” to mean temporary local variables.

7. Moving Features Between Objects

The author admits that he has trouble assigning responsibilities to objects. We are supposed to fix errors with “Move Method”, “Move Field”, “Extract Class” or other refactorings in this chapter.

8. Organizing Data

This chapter discusses a lot of different ways to simplify working with data. For instance, with these refactorings:

  • Replace Data Value with Object
  • Replace Array with Object

Also the refactoring “Replace Magic Number with Symbolic Constant” is explained a.k.a “Extract Constant”.

9. Simplifying Conditional Expressions

In my opinion the refactorings in this chapter need to be renamed. Apart from “Decompose Conditional”, which is clear enough. Although “Breaking up Conditional” might have been better.

10. Making Method Calls Simpler

Make method calls simpler by renaming them or replacing long parameter lists by objects. The latter technique could be a problem in concurrent programs. It is common to pass immutable values as parameters. You might not be able to replace them by immutable objects.

11. Dealing with Generalization

Generalization or in OO terms inheritance is a powerful mechanism, that tends to be overused a lot. You can push/pull a method or a field. Inheritance can be replaced by delegation and vice versa.

12. Big Refactorings

In this chapter starts what we can call the “dessert” part. No more simple refactoring recipes. Instead four refactorings that take a considerable amount of time.

13. Refactoring, Reuse and Reality

This chapter is an essay on Refactoring by William Opdyke.

14. Refactoring Tools

IDE’s have come a long way since this book was written. Most of the issues in this chapter are no longer valid.

15. Putting It All Together

This final chapter by Kent Beck is a bit mystical and vague. Those are his own words by the way. Some of the points make sense, but the chapter is written too much in a master talking to an apprentice style.

The book has a list of soundbites at the end. Literally. The fun thing is that you probably have heard or are going to hear a lot of these soundbites. “Refactoring” is a very useful book, albeit too focused on Java. Most recipes would work for another Object Oriented language. I give this book 5 stars out of 5.

Share
Posted in books | Leave a comment

NumPy Project Euler Problem 3

This entry is part 3 of 9 in the series NumPy Project Euler

Project Euler Problem 3 seems almost impossible to crack. However, using the right algorithm – Fermat’s factorization method and NumPy, it becomes very easy. The idea is to factor a number N into two numbers c and d.

   N = c * d  = (a + b) (a - b) = a ** 2 - b ** 2

We can apply the factorization recursively, until we get the required prime factors.

1. Create array of trial values

The algorithm requires us to try a number of trial values for a. It makes sense to create a NumPy array and eliminate the need for loops. However, you should be careful to not create an array that is too big. On my system an array of a million elements seems to be just the right size.

   a = numpy.ceil(numpy.sqrt(n))
   lim = min(n, LIM)
   a = numpy.arange(a, a + lim)
   b2 = a ** 2 - n

2. Get the fractional part of the b array

We are now supposed to check whether b is a square. Use the NumPy modf function to get the fractional part of the b array.

   fractions = numpy.modf(numpy.sqrt(b2))[0]

3. Find 0 fractions

Call the NumPy where function to find the indices of 0 fractions.

   indices = numpy.where(fractions == 0)

4. Find the first occurrence of a 0 fraction

Actually we only need the first occurrence of a 0 fraction. First, call the NumPy take function with the indices array from the previous step to get the a values of 0 fractions. Now we need to “flatten” this array with the NumPy ravel function.

   a = numpy.ravel(numpy.take(a, indices))[0]

Below is the entire code needed to solve the problem.

import numpy
 
#The prime factors of 13195 are 5, 7, 13 and 29.
 
#What is the largest prime factor of the number 600851475143 ?
 
N = 600851475143
LIM = 10 ** 6
 
def factor(n):
   #1. Create array of trial values
   a = numpy.ceil(numpy.sqrt(n))
   lim = min(n, LIM)
   a = numpy.arange(a, a + lim)
   b2 = a ** 2 - n
 
   #2. Check whether b is a square
   fractions = numpy.modf(numpy.sqrt(b2))[0]
 
   #3. Find 0 fractions
   indices = numpy.where(fractions == 0)
 
   #4. Find the first occurrence of a 0 fraction
   a = numpy.ravel(numpy.take(a, indices))[0]
   a = int(a)
   b = numpy.sqrt(a ** 2 - n)
   b = int(b)
   c = a + b
   d = a - b
 
   if c == 1 or d == 1:
      return
 
   print c, d
   factor(c)
   factor(d)
 
factor(N)

The code prints the following output:

1234169 486847
1471 839
6857 71

Have a go

I would suggest writing an unit test to check the outcome. Use for example trial division. To get you started here is some of the code of a possible test function. Please fill in the dots.

def is_prime(n):
   a = numpy.arange(2, n)
   return len(a[...]) == 0

Also maybe you should try finetuning the size of the trial values array, but be careful.

 

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly.

Share
Posted in programming | Tagged , | Leave a comment

2 Beautiful Computer Graphics Books

These Computer Graphics books are beautiful, because they have lots of color plates and figures, hardcover with nice designs. I think you should appreciate these kind of things, otherwise you could just as well hold a stack of paper in your hands or read an ebook.

Computer Graphics Books

1. Interactive Computer Graphics: A Top-Down Approach With Opengl

Author Edward Angel
ISBN-10 0201855712

This is an introductory book about OpenGL. We had an optional course on OpenGL in University and this book was required reading for this course.

Chapter 1 introduces some basic concepts, such as camera models and ray tracing.

Chapter 2 gets us started wih the OpenGL API.

Chapter 3 discusses input devices, the client-server perspective and menus.

Chapter 4 starts with a bit of geometry and linear algebra. This is followed by an OpenGL example. The chapter ends with transformations supported by OpenGL.

Chapter 5 deals with projections and perspective.

Chapter 6 is about light, light sources, reflection and ray tracing.

Chapter 7 involves studying implementation algorithms for geometric transformations, clipping and rasterization.

Modeling the real world is the topic of Chapter 8. This includes physical models based on elementary Newtonian mechanics.

Chapter 9 teaches us about curves and splines.

Chapter 10 mentions texture mapping, bit and pixel operations, composition techniques, sampling and aliasing.

The chapter titles could have been a bit longer and more descriptive. The book goes over some mathematics and theory. However, it never gets very challenging. I would not buy the book for that. There are lots of OpenGL examples. This is the strength of the book. Although the book has color plates and a nice hard cover, the code does not have syntax highlighting. You might say that I am too used to IDE’s, but I have seen syntax highlighting in at least one book, so it should be possible. I give this book 3 stars out of 5.

2. Computer Graphics: Principles and Practice in C (2nd Edition)

Author James D. Foley, Andries van Dam, Steven K. Feiner and John F. Hughes
ISBN-10 0201848406

Computer Graphics is about computer graphics and principles. This book has four authors, who are experts in their field. It has a hardcover, is richly illustrated with color plates and lots of figures. If the code had syntax highlighting, then it would have been even better.

Chapter 1 covers the basics. Chapter 2 is about SGRP (Simple Raster Graphics Package). Chapter 3 presents basic raster graphics algorithms for drawing 2D primitives.

Chapter 4 describes graphics hardware. Chapter 5 introduces geometrical transformations. Chapter 6 discusses viewing in 3D, projections and perspective.

Chapter 7 is dedicated to SPHIGS (Simple Programmer’s Hierarchical Interactive Graphics System). Chapter 8 is the first of three chapters on GUI’s. These three chapters are low on mathematics and code. Dialogue design is the title of Chapter 9. Chapter 10 examines user interface software.

Chapter 11 is about representing curves and surfaces. Finally, we get back to some code and mathematics. Chapter 12 builds on the previous chapter and continues with solid modeling. Sadly, no code in this chapter. Achromatic and colored light is the subject of chapter 13. This is a fun chapter, however, the lack of color usage in this chapter about color seems paradoxical to me.

In Chapter 14 we embark on a quest for visual realism. Chapter 15 is about visible-surface determination. Chapter 16 discusses illumination and shading.

Chapter 17 explores image manipulation and storage. Chapter 18 discusses advanced raster graphics architecture. Chapter 19 describes advanced geometric and raster algorithms.

Chapter 20 concentrates on advanced modeling techniques. Chapter 21 brings to life animation.

As I said above, this book is written by four authors. Obviously, this means that the writing style differs between chapters. Some chapters have no code or mathematics. Some chapters have one or the other. These are not my favorite chapters. The code examples are written in C or sometimes in pseudocode that looks a lot like C, so you need to have some knowledge of C.

The book is quite thorough and seems pedagogically sound. It is considered a “classic” for many reasons. Personally, I learned a lot about computer graphics algorithms. I give this book 4 stars out of 5.

Share
Posted in books | Leave a comment

NumPy Project Euler Problem 2

This entry is part 2 of 9 in the series NumPy Project Euler

Project Euler Problem 2 is definitely harder than Problem 1. This one requires reading the Fibonacci numbers Wikipedia article.

I read that the Project Euler problems are some sort of katas. Katas are what martial artist call exercises, preparing you for real-life fighting situations. For instance, imagine that your house is on fire and that your family is surrounded by heavily armed thugs. What do you do? In this case your secret weapon is NumPy, which is like the Dim Mak death touch. If you master that like me, then you don’t even need to bother with katas.

1. Calculate the golden ratio

When the bad ninja guys are walking towards you, the first thing to do, is calculate the golden ratio, also called the golden section. This part of the kata is called Moro Ashi Dachi.

1
2
phi = (1 + numpy.sqrt(5))/2
print "Phi", phi

This prints the golden mean.

1
Phi 1.61803398875

2. Find the index below 4 million

Next in the kata we need to find the index below 4 million. A formula for this is given in the Wikipedia page. All we need to do is convert log bases. We don’t need to round the result down to the closest integer. This is automatically done for us in the next step of the kata.

1
2
n = numpy.log(4 * 10 ** 6 * numpy.sqrt(5) + 0.5)/numpy.log(phi)
print n

We get the result 33.2629480359. You can double check this yourself from the Wikipedia article.

3. Create an array of 1-n

No, you say. Not the arange function again. We know about it already, when are we going to learn about the Jyutping. Patience, let’s first master the basics.

1
n = numpy.arange(1, n)

4. Compute Fibonacci numbers

There is a convenient formula we can use to calculate the Fibonacci numbers. We will need the golden ratio and the array from the previous step in the kata as input parameters. Print the first 9 Fibonaci numbers to check the result. I could have made an unit test instead of a print statement. This variation of the kata is left as an exercise for the reader.

1
2
fib = (phi**n - (-1/phi)**n)/numpy.sqrt(5)
print "First 9 Fibonacci Numbers", fib[:9]

The output is below. You can plug this right into an unit test, if you want.

1
First 9 Fibonacci Numbers [  1.   1.   2.   3.   5.   8.  13.  21.  34.]

5. Convert to integers

This step is optional. I think it’s nice to have an integer result at the end. It’s like the difference between Mawashi Geri and Mawashi Geri Koshi. The effect is almost the same, but one feels better than the other. OK, I actually wanted to show you the astype function.

1
2
fib = fib.astype(int)
print "Integers", fib

A long list of numbers is printed.

1
2
3
Integers [      1       1       2       3       5       8      13      21      34
  ... snip ... snip ...
  317811  514229  832040 1346269 2178309 3524578]

6. Select even-valued terms

The kata demands that we select the even valued terms now. This should be easy for you, if you followed the previous kata.

1
2
eventerms = fib[fib % 2 == 0]
print eventerms

There we go. Another opportunity to write unit tests.

1
2
[      2       8      34     144     610    2584   10946   46368  196418
  832040 3524578]

7. Sum the selected terms

This is the final step. Your enemies are lying on the ground helpless, unable to fight back. Finish the job, call the ndarray sum method.

1
print eventerms.sum()

For completeness, below is the complete code of this kata.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy
 
#Each new term in the Fibonacci sequence is generated by adding the previous two terms. 
#By starting with 1 and 2, the first 10 terms will be:
 
#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
 
#By considering the terms in the Fibonacci sequence whose values do not exceed four million, 
#find the sum of the even-valued terms.
 
#1. Calculate phi
phi = (1 + numpy.sqrt(5))/2
print "Phi", phi
 
#2. Find the index below 4 million
n = numpy.log(4 * 10 ** 6 * numpy.sqrt(5) + 0.5)/numpy.log(phi)
print n
 
#3. Create an array of 1-n
n = numpy.arange(1, n)
print n
 
#4. Compute Fibonacci numbers
fib = (phi**n - (-1/phi)**n)/numpy.sqrt(5)
print "First 9 Fibonacci Numbers", fib[:9]
 
#5. Convert to integers
# optional
fib = fib.astype(int)
print "Integers", fib
 
#6. Select even-valued terms
eventerms = fib[fib % 2 == 0]
print eventerms
 
#7. Sum the selected terms
print eventerms.sum()

That was quite a workout. More katas to come soon. Please stay tuned.


If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly. I would like to thank Christopher Felton and Gael Varoquaux for their recent reviews of my book.

Share
Posted in programming | Tagged , | Leave a comment

3 Great Computer Science Books in My Library

Here are 3 amazing (if a bit old) Computer Science books from my library.

3 CS Books

1. TCP/IP Illustrated Volume 1

Author W. Richard Stevens
ISBN-10 0201633469

This is a fantastic textbook about different network protocols. The protocols are illustrated using the output of tcpdump and other utilities. Protocols discussed include:

  • IP
  • ARP
  • RARP
  • ICMP
  • UDP
  • IGMP
  • TFTP
  • BOOTP
  • TCP
  • SNMP
  • FTP
  • SMTP

Beside the protocols several diagnostic tools such as ping and traceroute are covered in detail. The book contains lots of diagrams, that illustrate the protocols even further. Each chapter ends with exercises. The solutions of some of those exercises can be found in the book as well. I give this book 4 stars out of 5.

2. Database System Concepts

Authors Abraham Silberschatz, Henry Korth, S. Sudarshan
ISBN-10 0071122680

This book teaches about the inner workings, the nuts and bolts of databases without requiring a lot of prior knowledge:

  • Data Models
  • Relational Databases
  • Object Based Databases and XML
  • Data Storage and Querying
  • Transaction Management
  • Database System Architecture

Personally I feel that some material doesn’t belong here, such as XML, but it seems to be a tradition to include it. I found the chapter on indices quite interesting.

At the end of the book there are several chapters on popular commercial databases. It seems that these chapters do not add any value. In any case the authors should have devoted at least one chapter to an open source database. In the real world you have as much chance to work with a commercial database as with an open source one.

My copy is quite outdated by now. New developments, for instance NoSQL databases, are not mentioned. I give this book 3 out of 5 stars.

3. Machine Learning

Author Tom Mitchell
ISBN-10 0071154671

This is an introductory book on Machine Learning. There is quite a lot of mathematics and statistics in the book, which I like. A large number of methods and algorithms are introduced:

  • Neural Networks
  • Bayesian Learning
  • Genetic Algorithms
  • Reinforcement Learning

The material covered is very interesting and clearly explained. I find the presentation, however, a bit lacking. I think it has to do with the chosen fonts and lack of highlighting of important terms. Maybe it would have been better to have shorter paragraphs too.

If you are looking for an introductory book on machine learning right now, I would not recommend this book, because in recent years better books have been written on the subject. These are better obviously, because they include coverage of more modern techniques. I give this book 3 out of 5 stars.



Thanks for the NumPy 1.5 Beginner’s Guide review by Chris Fonnesbeck on the Strong Inference Blog.

Share
Posted in books | Leave a comment

NumPy Project Euler Problem 1

This entry is part 1 of 9 in the series NumPy Project Euler

Project Euler is a website that lists a number of mathematical problems, which are perfect to be solved with NumPy. Let’s start the new year with Problem 1.

1. Call the arange function

Call the arange function in order to store all the integers from 1 to 1000 in an array.

# 1. Numbers 1 - 1000
a = numpy.arange(1, 1000)

2. Select the multiples of 3 or 5

Select using the [] operator.

# 2. Select multiple of 3 or 5
a = a[(a % 3 == 0) | (a % 5 == 0)]
print a[:10]

This prints as expected

[ 3  5  6  9 10 12 15 18 20 21]

3. Sum the array elements

Call the sum method on the NumPy array.

# 3. Sum the numbers
print a.sum()

Once again the code below in its entirety.

import numpy
 
#Problem 1.
#If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
#The sum of these multiples is 23.
 
#Find the sum of all the multiples of 3 or 5 below 1000.
 
# 1. Numbers 1 - 1000
a = numpy.arange(1, 1000)
 
# 2. Select multiple of 3 or 5
a = a[(a % 3 == 0) | (a % 5 == 0)]
print a[:10]
 
# 3. Sum the numbers
print a.sum()

 

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly.

Share
Posted in programming | Tagged , | Leave a comment

4 Great Physics books I had to read

I made a list of four great introductory level Physics books I needed to read for my Master’s degree. You can see them below.

Stack of Physics Books

1. Analytical Mechanics

Authors Grant R. Fowles and George L. Cassiday
ISBN-10 0030989744

Mechanics was one of the first (if not the first) courses I followed in University. “Analytical Mechanics” is a brilliant junior level book that covers:

  • Newton’s Laws of Motion
  • Harmonic motion
  • Kepler’s Laws
  • Rigid bodies
  • Lagrangian Mechanics

The authors use real world examples such as falling raindrops to illustrate various concepts. The problems at the end of each chapter are quite challenging and interesting. There are also some numerical problems in the book, that should be solved with a computer. In my opinion there could have been more of those. The study group, that I was part of, ignored the “computer” problems completely.

2. Optics

Author Eugene Hecht
ISBN-10 020111609X

I had a course on Optics in my first year in University with this book. The book has lots of examples and is very “wordy”. I suspect the author hoped to make it a reference as well as a study book. The number of photos in “Optics” is unusually high for a Physics book. I wouldn’t mind if some of those were colour photos, but then again it would have made the book less affordable. Topics include:

  • Mathematics of Waves
  • Light
  • Geometrical Optics
  • Polarization
  • Interference
  • Diffraction

Overall, I would say that this was a good book. Ideal for people who prefer long explanations.

3. Introduction to Electrodynamics

Author David J. Griffiths
ISBN-10 013805326X

This book is an introduction on electricity and magnetism covering:

  • Electrostatics
  • Electric Fields
  • Magnetostatics
  • Magnetic Fields
  • Radiation
  • Relativistic Electrodynamics

The style of the book is very concise. In my opinion the majority of the examples are a bit abstract. Still the explanations are clear and the main points are indicated in such a way that you cannot miss them. For instance, in the story about Faraday’s law, the sentence

“A changing magnetic field induces an electric field.”

is printed in bold with a box around it and lots of whitespace. To be honest, I first tried winging it, only reading handouts and lecture notes. This was a big mistake. Having this book in my possession was a huge improvement.

4. Mathematical Methods for Physicists

Authors George B. Arfken and Hans J. Weber
ISBN-10 0120598167

As far as I remember this book was required reading for the “Special Functions” course. You could argue that this is not a pure Physics book. However, it is certainly not a pure Mathematics book, since the examples are related to Physics problems:

  • Vector Analysis
  • Group Theory
  • Infinite Series
  • Complex Algebra
  • Special Functions

I think that this is an awesome book. Sometimes I just read it for fun. Seriously. I can’t understand, how people can be so negative about it. Probably they were forced to go through the material at high speed without much help from their professors. I admit that this is not a good approach, especially given the size of the book.

Share
Posted in books | Leave a comment