Monday, December 28, 2009

Corrupted Git

Well, I have a directory with all my undergraduate project and I use git to ease my work, but recently I was not able to create a backup in a external disk because a error:


remote: Counting objects: 313, done.
remote: Compressing objects: 100% (116/116), done.
fatal: pack has bad object at offset 11893474: inflate returned 1
fatal: index-pack failed


I have files of almost 500MB on this repo, I suppose that the problem. I try to repack but it was useless, I also can check the hash of the object (with fsck), but I can't find where is that file, or what commit it belongs to.

This code will help in this case:


git checkout master
git branch new_master
git checkout new_master
for i in $!$(seq 1 100); do git checkout new_master~$!$i || break; done
for j in $!$(seq $!$i 100); do git checkout new_master~$!$j && break; done
# The error is between new_master~$!$j and new_master~($!$i-2)
git checkout master
git branch -D new_master
git branch new_master
git checkout new_master
git rebase --onto new_master~$!$j new_master~$!$((i-2)) new_master


It works (al least for my problem!!!), and now i want to rebase master:

git checkout master
git rebase new_master
git branch -d new_master


That's all.

Thursday, December 03, 2009

Compute the normal vector of a hyperplane in $R^n$

Well, basically any vector $N$ that holds $N \dot (p_i - p_j) = 0$ for for any vector $p_i, p_j \in H$ $H$ is the hyperplane.

Only $n$ different points are needed, call $P$ the set of $n$ points in $H$, and define $P^* = \{ p^*_i = p_i - p_{i+1} : p_i,p_{i+1} \in H , i \in [1,n-1]\}$.

$P^*$ is the set of vectors on $H$.

Any normal vector $N$ that holds that $N \dot p^*_i , i \in [1,n-1]$ is a normal vector.

$N$ has $n$ components, so we got here $n$ unknowns. And with the $n_1$ $p^*_i$ vectors we got $n-1$ equations.

An extra equation could be defined if we want a normalized vector, but this add a quadratic term in the systems, as the vector with all zero components can't be a normal vector of any hyperplane at least one of its components has a non-zero value, suppose that the $j$ component of $N$ has value $1$. and we have then a Ax = B, with n-1 equations and unknowns. we just have to try which j makes the systm non singular and solve it.


The system of equations:
$\begin{pmatrix}
p^*_{1,1} & p^*_{1,2} & \cdots & p^*_{1,j-1} & p^*_{1,j+1} & \cdots & p^*_{1,n} \\ p^*_{2,1} & p^*_{2,2} & \cdots & p^*_{2,j-1} & p^*_{2,j+1} & \cdots & p^*_{2,n} \\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
p^*_{n,1} & p^*_{n,2} & \cdots & p^*_{n,j-1} & p^*_{n,j+1} & \cdots & p^*_{n,n} \\
\end{pmatrix} \begin{pmatrix} N_1 \\ N_2 \\ \vdots \\ N_{j-1} \\ N_{j+1} \\ \vdots \\ N_n \end{pmatrix} = - \begin{pmatrix} p^*_{1,j} \\ p^*_{2,j} \\ \vdots \\ p^*_{(j-1),j} \\ p^*_{(j-1),j} \\ \vdots \\ p^*_{n,j} \end{pmatrix}
$

Start checking for $j \in [1,n]$ and you will get at much $n$ and at least 1 normal vector.

Python:


from numpy import array, dot
from numpy.linalg import solve, norm


def normal(P):
n = len(P)

if n < 1:
raise Exception, "O-size list."

m = len(P[0])

if array([len(p) != m for p in P]).any():
raise Exception, "Inconsistent set of points."

if n != m:
raise Exception

P_ = [p-q for p,q in zip(P[:-1],P[1:])]

N = []

for i in range(n):
try:
x = solve(
array([list(p)[:i] + list(p)[i+1:] for p in P_]),
array([-p[i] for p in P_])
)

x = array(list(x)[:i] + [1] + list(x)[i:])
N.append(x)

except:
pass

n = N[array([(dot(n,n)-1)**2 for n in N]).argmin()]

return n / norm(n)



if __name__ == '__main__':
print normal([array([1,0,0]), array([0,1,0]), array([1,1,0])])
print normal([array([1,123,0123]), array([0345,1,034]), array([132,134,034])])
print normal([array([1,0,0]), array([0,1,0]), array([1,123,1123])])
I'm gonna create a image with Debian and Django to upload it to AWS.



dd if=/dev/zero of=debian-ami count=1000 bs=1m
sudo mkfs.ext3 -F debian-ami
mkdir /tmp/chroot
sudo mount -o loop debian-ami /tmp/chroot
sudo debootstrap --arch i386 lenny /tmp/chroot/ http://ftp.debian.org
sudo chroot /tmp/chroot/
# Inside the chroot
mount -t proc none /proc
cd /dev
MAKEDEV console
MAKEDEV std
echo -e 'auto lo\niface lo inet loopback\nauto eth0\niface eth0 inet dhcp' >> /etc/network/interfaces
echo -e 'proc /proc proc defaults 0 0\n/dev/sda1 / reiserfs defaults 0 1\n/dev/sda2 swap swap defaults 0 0' > /etc/fstab
aptitude update
aptitude install locales-all
aptitude install ssh
exit

# In your machine
sudo umount -l /tmp/chroot
export EC2_PRIVATE_KEY=xxxxxxx.pem
export EC2_CERT=xxxxxxxxxxxxxxxxxx.pem
export EC2_ACCNO=xxxxxxxxxx
export ACCESS_KEY=xxxxxxxxxxxxxxxx
export SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ec2-bundle-image -i debian-ami --cert EC2_CERT --privatekey EC2_PRIVATE_KEY -u EC2_ACCNO
ec2-upload-bundle -b 20091203-linux-debian-lenny -m /tmp/debian-ami.manifest.xml -a ACCESS_KEY -s SECRET_KEY
ec2-register -n 20091203-linux-debian-lenny 20091203-linux-debian-lenny/debian-ami.manifest.xml