Skip to content

Instantly share code, notes, and snippets.

View jesuscast's full-sized avatar

Andres Castaneda jesuscast

View GitHub Profile
@jesuscast
jesuscast / simpsons_rule.py
Last active August 29, 2015 13:56
Calculates simpson's sum, receives the interval and the the number of intervals. An extra y function needs to be created separetely. Uses numpy. This function takes the results of the y function
from numpy import *
def simpsonSum(xRange,n):
tot = 0
width = 0
width = (xRange[1]-xRange[0])/n
x = [ interval for interval in arange(xRange[0],xRange[1]+width, width) ]
yRange = [ y(n) for n in x ]
tot += yRange[0]+yRange[n]
tot += sum([ 2*yRange[i] if (i%2==0) else 4*yRange[i] for i in range(1,n) ])
tot = tot*width/3
@jesuscast
jesuscast / trapezoidalSum.py
Last active August 29, 2015 13:56
Calculate the trapezoidal sum of a function over an interval. An external y function needs to be created. Uses numpy
def trapezoidalSum(xRange,n):
tot = 0
width = 0
width = (xRange[1]-xRange[0])/n
x = [ interval for interval in arange(xRange[0],xRange[1]+width, width) ]
yRange = [ y(n) for n in x ]
tot += yRange[0]+yRange[n]
tot += sum([ 2*yRange[i] for i in range(1,n) ])
tot = tot*width/2
return tot
@jesuscast
jesuscast / Pattern_Counter.py
Last active August 29, 2015 14:02
Counts the number of times a pattern appears on a list
def count_pattern(pattern, lst):
lenP = len(pattern)
#print("lenP",lenP)
lenL = len(lst)
#print("lenL",lenL)
total = 0
if(lenL >= lenP):
for i in range(0, lenL):
#print("repetition #:"+str(i))
match = False
@jesuscast
jesuscast / expression_depth.py
Last active August 29, 2015 14:02
Finds the depth of a mathematical expression. It follows the syntax expr = ('operator','term1','term2',...,'termN')
def depth(expr):
depthLevel = 0
if(isinstance(expr,(list,tuple))==False):
return 0
else:
depthLevel += 1
if( isinstance(expr[1],(list,tuple)) ):
biggestDepth = 0
for i in range(1,len(expr)):
depthOfCurrentTerm = depth(expr[i])
@jesuscast
jesuscast / tree_branches.py
Last active August 29, 2015 14:02
Tree branches. Defines a tree as a tuple and then access the branches with 0 based index, the nesting is defined as another tuple.
import math
def tree_ref(tree, index):
if(isinstance(index,(list,tuple)) and (len(index)>1)):
#print tree
#print index
#print "--1---"
newTree = tree[index[0]]
#print newTree
newIndex = tuple([index[k] for k in range(1,len(index))])
#print newIndex
@jesuscast
jesuscast / polynomialdivision.php
Last active August 29, 2015 14:02
Calculates de division of two polynomials. ENTRY an array where the index i equals the term with degree i. EX array(1,4,3) = 3x^2+4x+1
<?php
function degree($polynomial){
for($i = (count($polynomial)-1); $i>=0; $i--){
if($polynomial[$i]!=0){
return $i;
}
}
return 0;
}
function formatPolynomial($poly){
@jesuscast
jesuscast / highestCommonFactor.php
Last active August 29, 2015 14:02
Calculates the highest common factor in an array
<?php
function highestCommonFactor($array){
$hcf = 1;
$totalItems = count($array);
$smaller = $array[$totalItems-1];
for($i = 0; $i<$totalItems; $i++){
$temp = $array[$i];
if($array[$i]<0) {
$temp = -1*$temp;
@jesuscast
jesuscast / bestFit.py
Last active August 29, 2015 14:05
Given a set of x values and y values, this functions returns another function with the best fit line
def LFIT(x,y):
N = len(x)
if N != len(y):
return 0
Sx = sum(x)
Sy = sum(y)
Sx2 = sum([n**2 for n in x])
Sxy = sum([x[i]*y[i] for i in range(N)])
b = (N*Sxy-Sx*Sy)/(N*Sx2-Sx**2)
a = (Sy-b*Sx)/N
@jesuscast
jesuscast / sqrt-of-two.py
Created September 2, 2014 01:20
Calculates the sqrt of 2 with exaggerate precision
from decimal import *
getcontext().prec = 100
def powerSeries(A,N, precision):
a = Decimal(A)
n = Decimal(N)
power = Decimal(0)
total = Decimal(0)
for i in range(precision):
totalLocal = Decimal(1)
nTotal = Decimal(0)
@jesuscast
jesuscast / pi.py
Created September 9, 2014 17:52
Calculates pi
from decimal import *
getcontext().prec = 100
def calcPi(precision):
sign = 1.0
denominator = 1
sum = Decimal(0.0)
for i in range(precision):
sum += Decimal(1.0)/Decimal(denominator)*Decimal(sign)
sign = -sign