# Sage Fever

Well, I should be studying Calculus, but instead I wrote another sage application for visualizing another project problem. This one is the classic cow tied to a silo problem; if the rope just reaches the far side of the silo, how much area can the cow cover?

My python is still pretty rusty, but I think I did a little better on this one than the last:

###################################################################
#
# an application for plotting "the cow tied to a silo" problem.
# author: bnr
# date: 2012-10-16
#
###################################################################
from sage.plot.line import Line
from sage.plot.arc import Arc
r,t, a1, labl1,label2 = var('r,t, a1,labl1,labl2')
# the radius of the silo
r = 1
@interact
def _( r = slider( [1..25],1, label = "Silo Radius") ):

#the area labels
labl1 = text(('$\\frac{A_1}{2}$'), (2.5*r,1.5*r),fontsize=20,color='black')
labl2 = text(('$A_2$'),(.5*r,1.5*r),fontsize=20,color='brown')

# x and y on the silo perimeter
def x1(t): return r*cos((t))
def y1(t): return r*sin((t))
#the area of the half-circle; A_1
f = sqrt( (pi*r)^2 - (x -r)^2 )
L1 = plot(f, r, r*(pi+1), fill=True) + labl1
# the curve of the silo wall
L2 = parametric_plot( (x1(t), y1(t) ), (t, 0, float(pi)), color='red')

# wrapping the rope around the silo

# finding the curve as the rope is stretched taught
def new_x(a) :
new_x = x1(a) - abs( r*(pi-a)*sin(a) )
return new_x

def new_y(a) :
new_y = y1(a) + ( r*(pi-a)*cos(a) )
return new_y

# the tangent line of the taught rope
s_tan = line( ( ( new_x(a), new_y(a) ) , ( ( x1(a),y1(a) ) ) ) )

# the arc walked by the cow with the rope taught
cow = parametric_plot( (new_x(t), new_y(t) ), (t, 0, float(pi)), color='brown')
return s_tan + cow + labl2

# calculating the area in the half-circle
def A1(a) :
return round( (pi^3)*(a^2)/2, 4)
# calculating twice the area of the integral part; A_2
def A2(a) :
return round( (pi^3)*(a^2)/3, 4)
# point of tangent to silo
def f(a):
p1 = point( ( x1(a), y1(a) ) )
return L1 + L2 + p1 + silo_rad(a)
# printing the calculated areas
print( html('$A_1= \\frac{\pi^3 r^2}{2} {\\approx}$' + str(A1(r) ) + '$\hspace{12 mm}2 \\times A_2 = \\frac{\pi^3 r^2}{3} {\\approx}$' + str(A2(r)) + '$\hspace{12 mm}Total\ Area = \\frac{5\pi^3 r^2}{6} {\\approx}$' + str(A2(r) + A1(r)) ) )

# animating the tangent line/integral part; A_2
a1 = animate([f(i) for i in srange(0,float(pi),.1)] , xmin = float(-2*r), xmax = float((1.1+pi)*r), ymin = float(-.2), ymax = float(1.1*pi*r))
a1.gif(savefile='my_animation.gif', show_path=False )

# Sage

I haven’t posted here in a while mostly because I have been preoccupied with calculus 2; between it and my other classes I don’t have a lot of time to code. However I found an intersection between calculus and coding with one of my assignments. Charged with determining if a ball could be caught by a person riding a ferris wheel, I decided to write a Sage Notebook application that animates the two trajectories with relation to time. The result is a little rough (as is my python), but it does work.

# set the global variable
a2,t,L1,L2,min_d= var('a2,t,L1,L2,min_d')

# default high minimum distance
min_d = 1000

# these fuctions return the x's and y's
def x1(t): return 10*t
def y1(t): return 10*sqrt(3)*t-4.9*(t^2)

def x2(t): return 25+7*cos((6+t)*(-pi/6))
def y2(t): return 7+7*sin((6+t)*(-pi/6))

# two lines from the functions
L1 = parametric_plot( (x1(t), y1(t) ), (t, 0, 6), color='green')

#L2 = parametric_plot( (x2(t), y2(t) ), (t, 0, 12))
L2 = circle( (25,7),7 )

# Calclulate the distance between the points
def distance(a):
d = float( sqrt( ( x2(a)-x1(a) )^2 + (y2(a)-y1(a))^2 ) )
return d

# record minimum distance
def min_distance(distance) :
global min_d
if (distance < min_d) :
min_d = distance

# find the points at 't' on the two lines
def f(a):
p1 = point( ( x1(a), y1(a) ) )
p2 = point( ( x2(a), y2(a) ) )

#calculate distance
d = distance(a)

# record minimum distance
min_distance(d)

return L1 + L2 + p1 + p2 + text("Distance: "+ str(distance(a)),(30,16)) + text("Min Distance: "+ str(min_d),(6,16))

# the animation
a2 = animate([f(i) for i in srange(2.1,6,0.1)], xmin = 0, xmax = 35, ymin = 0, ymax = 16)

# the slider and animation checkbox
@interact
def _(Time=(0,6), animate_it = checkbox(False, "Animate it")):

if (animate_it == True) :
a2.show(20,1)
else :
show(L1 + L2 + point( (x1(Time), y1(Time)) )+ point( (x2(Time), y2(Time)),xmin = 0, xmax = 35, ymin = 0, ymax = 16 )+ text("Distance: " + str(distance(Time)),(30,16)))



you can see the application in action here

# fedora 16

This weekend I made the switch from from Ubuntu 11.04 to Fedora 16 beta. After the last upgrade, Ubuntu started acting funny, everything from shutdown/reboot issues to glitchy UI with the new Unity desktop. I did like some of what Unity was doing, but I think it still needs a lot of work  before it is really ready; both in terms of the interface design and stability. Much of Ubuntu, like most distributions is the culmination of the efforts of the entire OS community, but the stuff that they layer on top they have to maintain themselves and I think Unity was such a big project that its release came at the cost of some of the normal “house keeping” tasks of running a linux distribution, and this may have caused some of the issues I and others have complained about (or it could simply be that to much had changed to smoothly upgrade from 10.10). I had been leaning towards giving the 11.10 release a chance to correct some of the shortcoming of the previous one but since I am taking a college course that emphasizes rpm distros, I thought it wouldn’t be a bad idea to give Fedora another go, besides, the brief look I gave gnome3 left me wanting more.

The computer I am using is  an Asus 1215t, with no optical drive and a 12″ screen  it is what some have termed “notbooks”, as in bigger than a netbook, but not really a full sized notebook. This model has the AMD neo low power cpu, integrated ATI graphic, typicall intel HD audio, and most notable a dreaded Broadcom 4313 wireless b/g/n. I think it bears mentioning that I have had little trouble with broadcom wireless in the past, though this may have just been luck. With Ubuntu wireless wouldn’t work until you otherwise connect to the internet and use the Proprietary drivers applet to install the correct driver.

Initially I installed Fedora 15, not wanting to use the beta, and also not wanting to wait the few weeks until it is finalized and  shipped, but I was not able to get the wireless card to work no matter what I tried. For this wireless card there are several drivers available: the old b43 driver that needs proprietary firmware (typically this is what Ubuntu uses), the newer broadcom-sta (aka wl) driver from Broadcom, the fallback ndiswrapper wich just wraps around an appropriate windows driver, and a newcomer the brcm80211 which is open source but still very much in the experimental stage. I tried everything, even compiling the wl driver from the source provide from Broadcom even though there are binaries in the Fedora repos. I never did get the brcm80211 driver to compile, but considering the shape it is currently in that is not a big surprise. It is likely that ndiswrapper would have worked had I not been using a 64bit distro, and the only available drivers are 32bit. I had just about given up hope when I decided to try the beta before running back to Ubuntu. The Fedora 16 beta didn’t have the wbroadcom-sta driver in the repositories, only the b43, which didn’t work. However, after installing the kernel-devel package I was able to compile the source provide by broadcom and it works perfectly.

I have only had a few days with the new gnome3 desktop, but so far I like it, even more than Unity. It is a little different, and takes getting used to but I think in the end it is an improvement over the old classic gnome interface. Some have complained that nothing in the GnomeShell (the default graphical environment that ships with gnome3) can be customized like in classic gnome, but gnome3 is just the framework, in time no doubt people will write their own graphic shells that use gnome3, or applications that allow the tweaking of elements within an installed shell. Since most of the shell is written in JavaScript (with a couple additional libraries) anyone with a little programming knowledge should be able to play with the interface, or write their own.

# found findbugs

I recently found findbugs, a static analysis tool for Java. I ran it on mdb2mysql and was surprised to only find one “serious” bug, a possible null pointer deference. I think it was actually a false positive since the value in question was checked against being null, but switching the code block to a try catch is the more stylistically correct way to handle it. Other than that there were a few performance issues with the way I put the sql statements together; I should have known better than to just concatenate strings without using StringBuffer explicitly. The beauty of static analysis is that findbugs can scan the code and find these little things as the code is being developed and remind me to tie up loose ends and sort out dirty code. I can see that using it and other tools should help me become a better programer.

However, most of the issues I need to tackle are not ones that static analysis will uncover: inelegant code, method misuse, unnecessary variables. A lot of these things are technically correct in that they work and can’t throw any errors, but that doesn’t mean they are easy to understand and maintain or that they are the most efficient way  to achieve the goal of the program.

And of course  that ignores the all issues of UI design. Currently both the CLI and GUI of mdb2mysql need lots of work; the program is functional, but the GUI is missing many “GUI” features such as tab focus switching, execute/next input  with ‘enter’ from text input, additionally I would like to eliminate the modal dialogs. The CLI also is extremely clunky; there are branches that trap the user into irreversible action were the only options are to move forward or kill the application.

# java

With two java courses under my belt I took some time over summer break to write a small java application. Mdb2Mysql is a tool for converting Access databases into MySQL databases. I used the Jackcess API from healthmarketscience.com to read the Access files. After playing around with the API for bit to get the hang of how I needed to use it I started with a CLI program that would list the tables and display the table schema of the open file. From there I moved on to have it write out Valid MySQL statements to create the new database to a text file. Then I added the MySQL login part and used the JDBC driver to send the statements one at a time to the server recreating the Access databases contents on the server.

Then it was Swing time. While I had hand coded all the Swing exercises in my java classes, it didn’t take me long to realize I needed to use a more full featured IDE than gedit (ok, gedit is not an IDE). So I spent some time just trying to figure out how to use NetBeans. I have to say Netbeans can be really slow sometimes. It was on one of those mornings that I was waiting for NetBeans to finally warmup that I started poking about on google and found the (perhaps bad) tip that OpenJDK performs significantly slower than Sun/Oracles JDK. So a Yak shaving I went. I downloaded the JDK directly form Sun/Oracle and installed it in /usr/local/etc/, which may not have been the right place for it( later moved it to /usr/lib/jvm/). Just for good measure I installed the latest NetBeans, version 7.0. I though that I was all ready to go, but while netbeans had no problem finding the new JDK the rest of the computer was somewhat confused. I though that If I removed the open JDK it would settle on the new one but that turned out not to be the case.

After uninstalling and reinstalling things numerous times I finally got it all working, but for a little while I had the unfortunate experience of having one JDK not compile and another not work at runtime. Not to be slowed down I for a little while I was composing the code and compiling it in netbeans and then testing it from the command line. In the end the problem was that each JDK had different libraries, when I got them all sorted out everything finally just worked.

# Third rails

My summer break is over, but in the brief few weeks I had I took a third run at Rails, or Ruby on Rails to be more precise. I had started with the example blog application from the online tutorial. It is a pretty good representation of how you can very quickly, with the use of scaffolds, get the basic structure of an application up and running. I even played around with it customizing the layout of the blog, although that is mostly just rewriting the html portion of the .html.erb files.

Then I went through the Depot application from Pragmatic Programmer’s Agile Web Development with Rails. It introduces a lot more features than the Blog application, and the most of the 700+ pages of the book are a pretty deep exploration of the Rails environment. In fact a lot of it was too deep for me to really understand the first time through.

I felt that I needed to explore on my own rather than simply following a guided track to producing a half finished sample application. I have had a project in mind for awhile, so after getting about halfway through Foundation Rails 2 I struck out on my own. Rails2 is a significantly different kind of book than Agile Web development, were Agile is a long deep look, even the depot application is fairly involved, Rails2 takes a number of quick strafing runs at the framework, each one designed to expose one or just a few aspects of the framework. The result is that any code unrelated to the lesson at hand is not present to possibly confuse the reader. I found that this provides a much clearer understanding of how Rails works. Undoubtedly this approach glosses over some details, but that is were the strength of Agile Development comes in, it provides the deep background to fill in any gaps from Rails 2.

About my application; I designed it to be written in three phases: first just a adaptable brochure-ware website. Rails is total overkill for this, but I figured it would be a good starting point as I developed my skills. Next I would add a public photo gallery option for the sites target; a photography business. The third and final stage adds private proofing galleries for customers to pick photographs for processing and ordering of prints, all automated through the site.

I accomplished most of the first phase, although the application is very brittle. I started out by thinking about my data structures for several hours, I still got them all wrong. I have added, subtracted and changed columns in the database a half dozen times as the application evolved. I still have several changes that need to be made, although I am thinking about taking what I have learned and just starting over from scratch. I have learned a lot, in fact I have already rewritten every original line of code several times (and no doubt need to again).

At this point the application has page objects that have associated image and text objects, these objects can be created and edited through an interface (still under construction) and they can be linked to new pages for navigation purposes. The objects are all shaped and position by using inline css values that are held in the database. An approved administrator can log in and edit the applications elements through the web interface. As I mentioned the application is still very brittle and I will likely start over from scratch now that I have a better idea of what the objects should look like and how the applications logic will work. So, until then, I have no code up, although I will eventually get it up on github, or some other code hosting site.

For now I have to get back to the Fall semester at school.

# A big Tree

In preparation for a test on the Windows Vista Operating System I decided to chart out the system links in the Windows Menu. So far I have completed the ‘System and Maintenance’ Branch of ‘Control Panel’. Skipping most of the ‘Help ‘ options I charted 65 destinations. A few of those are repeats – different paths to the same destination, but there are still 10 more branches in ‘Control Panel’. Granted, most of the endpoints in the remaining ‘Control Panel’ branches are already in the ‘System and  Maintenance’ branch (it is easily the largest branch). However, there are still some uncharted locations. Also, I simplified a little; For instance I counted ‘Device Manager’ as a single destination (endpoint) when it actually provides several windows for each device listed – these device windows are directly linked in other parts of ‘Control Panel’. Had I completely charted to each endpoint, even excluding ‘Help’ links, the count would have easily exceeded 100 destinations.

This little project was prompted by the ‘Microsoft Windows Vista Task Reference Recommended Method’ list in the back of my Windows Vista text book. The reference lists 251 different tasks with a single recommended method for each. since there are numerous ways to achieve each task, charting them all out is probably a ridiculous idea. I knew that it would be a big job, so I first searched around for one on the internet, but came up empty.

Just looking the little portion that I have charted, I can’t say that it really makes anything much clearer; this may be the real reason that nobody else thought to try this.