analytics

Friday, April 20, 2012

Quadratic surface fitting

At one time I came across the need to do something like, say I had these 9 z values like heights on a map...
For points (-1,-1),...(1,1)
What I want is some function in x and y that matches these heights. Today I thought of the way to do that. First, interpolate and find the three quadratic equations one for each row. Maple can do this with the polynomial interpolation command.
The 3 equations end up being:
Now the trick I use to link these 3 equations together smoothly is I think, I would like the first equation to be valid when y=-1, and not when y=0 or y=1. I can interpolate the 3 points (-1,1),(0,0),(1,0) and multiply the first equation by that curve, and similarly for the other two:
Then I can make the whole equation by adding the 3 equations in x each multiplied by their corresponding y equation:

it simplifies to:



(You might have to click and zoom in to have a look)
The graph is:
Examining it is does go through the 9 points smoothly. I think a similar method will work for any number of points producing a corresponding equation in x and y, the highest power being the number of points in one row minus one. 

Tuesday, April 17, 2012

Non-algorithmic floor and ceiling functions

It's always kind of annoyed me when you need just the first digit of a decimal or you want to round up to the next whole number that you always seem to have to use floor or ceiling functions that you can't use ordinary techniques with because they are sort of outside of usual calculus. So anyway I came up with these conversions:

Floor(t) = t-u(t) where u(t) =
You see, if your t = something like 4.673, at that point this function equals .673, so you just subtract that and get your floor value.

Then there's the Ceiling function or round up,
Ceil(t) = t+U(t) where U(t) =
 They are kind of messy but at least it gives you the power of floor and ceiling without stepping outside of calculus.
* One caveat is that the function is undefined at the exact whole numbers. I guess it depends on how you define floor whether the floor of 4.0 would be 3.0 or 4.0 but with these equations putting in whole numbers is equivalent to a divide by 0.
*If you look on wikipedia you can see the equations that led to these equations, they are the result of an infinite sum of sin waves (you have to scroll a ways down under "Series Expansion),
http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
but they don't mention that that infinite sum has a closed form and the problem they mention of the whole numbers giving a value halfway in between doesn't come up, these are instead undefined there.

Saturday, February 25, 2012

continuous binary matrix multiplication

Consider these pictures as 100x100 binary matrices, the third picture in the series is when you matrix multiply the first two and take every non-zero cell to be a 1.


Here's another one:




Here's one more:



Recursive matrix

I just wanted to see what this would look like you start with a 3x3 Matrix A:
A   A
  A  
A   A
So A is recursively defined. Each block is a smaller version of A. When you eventually on the computer get down to a size of one pixel you terminate the recursion with:
1      1
    1   
1      1
Where these 1's are black pixels. 
Here's what it looks like:

   

a type of complex plot

I thought of this way to visualize a polynomial with a complex domain and complex range. This is the polynomial x^10-1 with the real and imaginary component of x between -1 and 1 and -1i and 1i respectively. To show the complex output of certain values of x, I colored the pixel as more red the bigger the real component and more green the bigger imaginary component. In computers red and green light mixed together make yellow so bright yellow is where both the real and imaginary component of the function output are 1. Black areas are where the function is closer to -1 in both the real and imaginary component.

 For reference where the function equals 0 will have a color of:

Better would be to use a 4 component color system like CMYK and have more C to be more positive real and more M to be more positive imaginary and Y and K for more negative real and imaginary which could be printed on most printers but what you would see on the computer screen would just be an approximation because the computer only uses 3 components. Then the most black parts would be where the equation equals 0.


Thursday, February 16, 2012

Gravitational mass vs inertial mass

I've heard a lot of physicists and people interested in physics noting that inertial mass and gravitational mass seem to be different phenomena and there's no clear reason why the two should end up being exactly the same magnitude.
  First consider the following thought experiment. Suppose you have two perfectly hard spheres of the same mass that start out a distance apart and gravitationally attract to one another, when they eventually collide they both reverse direction and eventually end up in the same configuration that they started, motionless. All the energy was originally from their potential energy based on gravitational mass, turned into kinetic energy based on inertial mass, and then their gravitational attraction gradually slowed them down to return to the same state.All the while the system as a whole had the same total energy.
  Now if something could have more gravitational mass than inertial mass the balls would attract to one another and collide but not go back to the original state they would bounce a shorter and shorter distance apart each collision until they were eventually fused together. This would result in the final system having less energy than what it started with which in an isolated system can't happen.
  And the opposite is if the gravitational mass were less than the inertial mass then every time they collided they would end up ever farther apart and going faster to get there which would eventually end up with both masses having infinite kinetic energy.
  So you see this thought experiment shows that they have to be the same based on conservation of energy.

Wednesday, February 15, 2012

solving boggle puzzle in the newspaper

Starting with a puzzle like this:
N  N  O  L  H
O  E  H  Y  O
D  I  P  M  C
R  A  M  O  P
E  P  S  H  A

The idea is you make unique words length 3 or greater using each letter at most once and moving through the grid either up, down, left, right, or diagonally from letter to letter to spell out the word.
 I wrote a computer program that searched through a dictionary and found every possible word. It found 741. Here is a list of all the words of 7 or more letters it found:
 AMIDOLS
COMPARE
DAMPENS
EIDOLON
ENHALOS
HEIRDOM
HYPHENS
MADEIRA
MADONNA
MAIDENS
MIDRASH
NONPAID
OOMPAHS
PARAPHS
PARDONS
PERCOID
PHENOLS
POPEDOM
PRIMPED
RAMPION
RAPHIDE
SAPHENA
SARCOID
COMPADRE
DAMPENER
DIAPHONE
DRAMSHOP
EIDOLONS
LYCOPENE
LYMPHOMA
OLYMPIAD
RAMPIONS
SAMPHIRE
SAPHENAE
HOMOPHONE
LYMPHOMAS

My source code in Python:


import copy
class words():
    f = []
        
    def add(self, word):
        print(word)
        self.f.append(word)
    def shut(self):
        r = open("result.txt", "w")
        self.d = set(self.f)
        for word in self.d:
            r.write(word+'\n')
        r.write(str(len(self.f)) + '\n')
        r.close()
result = words()


def check(word, puzzle, pos, n):
    p = puzzle
    p[pos[0]][pos[1]][1] = False
    if n+1 == len(word):
        if len(word) > 2:
            result.add(word)
        return True
    try:
        if (p[pos[0]][pos[1]-1][0] == word[n+1]) and p[pos[0]][pos[1]-1][1] == True:
            check(word, copy.deepcopy(p), [pos[0], pos[1]-1], n+1)
    except:
        pass
    try:
        if (p[pos[0]-1][pos[1]][0] == word[n+1]) and p[pos[0]-1][pos[1]][1] == True:
            check(word, copy.deepcopy(p), [pos[0]-1, pos[1]], n+1)
    except:
        pass
    try:
        if (p[pos[0]-1][pos[1]-1][0] == word[n+1]) and p[pos[0]-1][pos[1]-1][1] == True:
            check(word, copy.deepcopy(p), [pos[0]-1, pos[1]-1], n+1)
    except:
        pass
    try:
        if (p[pos[0]][pos[1]+1][0] == word[n+1]) and p[pos[0]][pos[1]+1][1] == True:
            check(word, copy.deepcopy(p), [pos[0], pos[1]+1], n+1)
    except:
        pass
    try:
        if (p[pos[0]+1][pos[1]][0] == word[n+1]) and p[pos[0]+1][pos[1]][1] == True:
            check(word, copy.deepcopy(p), [pos[0]+1, pos[1]], n+1)
    except:
        pass
    try:
        if (p[pos[0]+1][pos[1]+1][0] == word[n+1]) and p[pos[0]+1][pos[1]+1][1] == True:
            check(word, copy.deepcopy(p), [pos[0]+1, pos[1]+1], n+1)
    except:
        pass
    try:
        if (p[pos[0]+1][pos[1]-1][0] == word[n+1]) and p[pos[0]+1][pos[1]-1][1] == True:
            check(word, copy.deepcopy(p), [pos[0]+1, pos[1]-1], n+1)
    except:
        pass
    try:
        if (p[pos[0]-1][pos[1]+1][0] == word[n+1]) and p[pos[0]-1][pos[1]+1][1] == True:
            check(word, copy.deepcopy(p), [pos[0]-1, pos[1]+1], n+1)
    except:
        pass
    
def main():
    puzzle = [[['N', True], ['N', True], ['O', True], ['L', True], ['H', True]], [['O', True], ['E', True], ['H', True], ['Y', True], ['O', True]], [['D', True], ['I', True], ['P', True], ['M', True], ['C', True]], [['R', True], ['A', True], ['M', True], ['O', True], ['P', True]], [['E', True], ['P', True], ['S', True], ['H', True], ['A', True]]]
    list = open("OWL.txt", "r").readlines()
    for w in list:
        word = w.split()[0]
        for i in range(0, len(puzzle)):
            for j in range(0, len(puzzle[i])):
                if puzzle[i][j][0] == word[0]:
                    inpuzzle = check(word, copy.deepcopy(puzzle), [i,j], 0)
    result.shut()