a better date range picker

•August 20, 2014 • Leave a Comment

I was unhappy with the range-datepickers I had seen so I made my own. I needed a date range picker that could select ranges of years, quarters or months, and I couldn’t find one that worked in this way. I had been using the jquery datepicker, but it didn’t have a “quarter” option; although it may have been possible to extend the datepicker to allow for this. at any rate, this is still a little rough and a little long in the code, but it works the way I wanted it to; the available months/quarters and years change in logical ways depending on the current selection in each of the other select menus. It also preserves the selected date through aggregate mode changes; a setting of March is still there when switched to “quarterly mode” and back to “monthly mode”.

date-range-picker

the date-range-picker in Google Chrome

Here’s the date-range-picker in chrome; appearance will vary depending on the browser and the implemented formating. This may be a reason to switch over to hidden select elements mirrored by styled divs in order to have cross-browser consistency and for increased visual adaptability.

Continue reading ‘a better date range picker’

netbeans 7.0.1 is broken

•May 29, 2014 • Leave a Comment

I just spent way too much time figuring out that Netbeans 7.0.1 (from the repos) is broken. If anybody else is struggling with it, stop and upgrade to the 7.1.2 version at netbeans.org. In particular, it was silently failing to integrate with Mercurial. Additionally, many of the configuration dialogs were malfunctioning. Hopefully this helps somebody.

python approximate intetegrator 2.0

•January 30, 2013 • Leave a Comment

A guy in calc class was working on a more complicated solution to the approximate integration problem, and so I felt inspired to make a few improvements:

#!/usr/bin/python

import sys
from math import *

def main(argv):
        try :
                if argv[0] == '-h':
                        print 'usage: apxint.py <function_of_x> <lower_bound> <upper_bound> <subinterval_width>';
                        sys.exit();
                else :
                        function_of_x = argv[0];
                        a = float(eval(argv[1]));
                        b = float(eval(argv[2]));
                        n = float(eval(argv[3]));
                        dx = (b-a)/n;

                        # trapezoid Rule
                        def f(x) :
                                return eval(function_of_x);
                        x = a;
                        t = 0.0;
                        while (x < b) :
                                t += f(x);
                                x += dx;
                                t += f(x);

                        ft = (dx/2)*t;
                        print ("Trapezoid Rule = " + str(round(ft, 6)));

                        # midpoint Rule
                        x = a + (dx/2);
                        m = 0.0;
                        while (x <= b) :
                                m += f(x);
                                x += dx;
                        print ("Midpoint Rule = " + str(round(dx*m, 6)));

                        # Simpsons Rule
                        x = a;

                        s = 0.0;
                        while (x < b) :
                                s += f(x);
                                x += dx;
                                s += 4.0*f(x);
                                x += dx;
                                s += f(x);

                        fs = (dx/3)*s;
                        print ("Simpson's Rule = " + str(round(fs,6)));
                        sys.exit(1);

        except IndexError:
                print 'usage: apxint.py <function_of_x> <lower_bound> <upper_bound> <subinterval_width>'
                sys.exit(2);

if __name__ == "__main__":
        main(sys.argv[1:])

calculator fatigue

•January 30, 2013 • Leave a Comment

tired of tediously punching away at my calculator doing approximate integration I was inspired to whip up a little python script for computing Trapezoid rule, Midpoint rule and Simpson’s rule:

import math
from math import sqrt, log
a,b,n = 0.0,1.0,4.0;
dx = (b-a)/n;

# trapezoid rule
def f(x) :
        return 34*(math.cos(x**2));
x = a;
t = f(x);
x += dx;
while ((x + dx) <= b) :
        tt = f(x);
        t += 2*tt;
        x += dx;
tt = f(x);
t += tt;
ft = (dx/2)*t;
print ("T = " + str(round(ft, 6)));

# midpoint rule
x = a + (dx/2);
m = 0.0;
while (x <= b) :
        mm = f(x);
        m += mm;
        x += dx;

print ("M = " + str(round(dx*m, 6)));

# Simpsons rule
x = a;
s = f(x);
x += dx;
while ((x + 3.0*dx) <= b) :
        ss = f(x);
        s += 4.0*ss;
        x += dx;
        ss = f(x);
        s += 2.0*ss;
        x += dx;

ss = f(x);
s += 4.0*ss;
x += dx;
ss = f(x);
s += ss;
fs = (dx/3)*s;
print ("S = " + str(round(fs,6)));

the cow and the silo redux

•November 18, 2012 • Leave a Comment

I really need to leave Sage Notebook alone and study, but I felt that I could improve the “cow” application:

Click to see the animation

here is the code:

Continue reading ‘the cow and the silo redux’

Sage Fever

•November 16, 2012 • Leave a Comment

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
 def silo_rad(a):

 # 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

•November 12, 2012 • Leave a Comment

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)))

parametric trajectories

parametric trajectories with respect to time

 
you can see the application in action here

 
Follow

Get every new post delivered to your Inbox.