Articles

Solve system of linear equations

In python on Jul 27, 2016 by theoryl Tagged: , , ,

Solving system of linear equations m x = y with NumPy

import numpy as np
from numpy import linalg as LA

m = np.array([
[  0.68,  0.597,  -0.33,],
[-0.211,  0.823,  0.536,],
[ 0.566, -0.605, -0.444,],
])

y = np.array([
[  0.108,   -0.27,   0.832,],
[-0.0452,  0.0268,   0.271,],
[  0.258,   0.904,   0.435,],
])

# Using QR decomposition
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.qr.html
q, r = LA.qr(m)
soln = np.dot(LA.inv(r), np.dot(q.T, y))

# Using least squares
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html
soln2 = LA.solve(m, y)

>>> print m
[[ 0.68   0.597 -0.33 ]
 [-0.211  0.823  0.536]
 [ 0.566 -0.605 -0.444]]
>>> print y
[[ 0.108  -0.27    0.832 ]
 [-0.0452  0.0268  0.271 ]
 [ 0.258   0.904   0.435 ]]
>>> print q
[[-0.74762435 -0.58417508 -0.31590713]
 [ 0.23198344 -0.67542676  0.69998741]
 [-0.62228733  0.45004241  0.64048444]]
>>> print r
[[-0.90954769  0.12107447  0.64735473]
 [-0.         -1.1769044  -0.3690698 ]
 [ 0.         -0.          0.19506751]]
>>> print soln
[[ 0.60907309  2.67396101  1.67163769]
 [-0.2309286  -1.56240961  0.07184063]
 [ 0.51001615  3.50162105  1.05334088]]
>>> print np.dot(m, soln)
[[ 0.108  -0.27    0.832 ]
 [-0.0452  0.0268  0.271 ]
 [ 0.258   0.904   0.435 ]]
>>> print soln2
[[ 0.60907309  2.67396101  1.67163769]
 [-0.2309286  -1.56240961  0.07184063]
 [ 0.51001615  3.50162105  1.05334088]]
>>> print np.dot(m, soln2)
[[ 0.108  -0.27    0.832 ]
 [-0.0452  0.0268  0.271 ]
 [ 0.258   0.904   0.435 ]]
Advertisements

Articles

Checkout subdirectory in a git repository

In howto on Jul 13, 2016 by theoryl Tagged: , , ,

To checkout a subdirectory in a Git repository using sparse-checkout, follow these instructions

git remote add -f origin https://github.com/whoever/whatever.git
git config core.sparsecheckout true
echo /path/to/subdirectory >> .git/info/sparse-checkout
git checkout master

Alternatively

git clone https://github.com/whoever/whatever.git
cd whatever
git config core.sparsecheckout true
echo /path/to/subdirectory >> .git/info/sparse-checkout
git read-tree -mu HEAD

Also see this StackOverflow answer.

Articles

How to sync a forked git repository?

In howto on Jul 13, 2016 by theoryl Tagged: , , ,

To synchronize your forked Git repository with the upstream, follow these nice instructions on StackOverflow:

# Add the remote, call it "upstream":

git remote add upstream https://github.com/whoever/whatever.git

# Fetch all the branches of that remote into remote-tracking branches,
# such as upstream/master:

git fetch upstream

# Make sure that you're on your master branch:

git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:

git rebase upstream/master

# If you don't want to rewrite the history of your master branch,
# then you should replace the last command with the following

#git merge upstream/master

# Now push your branch to your own forked repository

git push origin master

# If you've rebased your branch onto upstream/master you may need to
# force the push in order to push it to your own forked repository

#git push -f origin master

Articles

Pack and unpack tar.gz files

In howto on Jul 7, 2016 by theoryl Tagged:

I got these commands from https://arxiv.org/help/tar and https://arxiv.org/help/unpack

To pack:

tar -cvzf mystuff.tar.gz foo.tex fig1.eps fig2.eps

To unpack:

gunzip mystuff.tar.gz
tar -xvf mystuff.tar

Or more simply:

tar -xvzf mystuff.tar.gz

For additional humor, see https://xkcd.com/1168/ about tar commands.

Articles

Use custom class as C++ map key

In howto on Jul 5, 2016 by theoryl Tagged: ,

To be able to use a custom class as the key in C++ std::map, a less-than operator (operator<) must be defined for the custom class. One way is to define it in the class itself:

class MyClass{
public:
    MyClass(int x, int y) : _x(x), _y(y) {}
    ~MyClass() {}

    bool operator<(const MyClass& other) const{
        return (_x < other._x) || (!(other._x < _x) && (_y < other._y));
    }

private:
    int _x, _y;
};

Alternatively, one can define a comparison function (in the global scope):

class MyClass{
public:
    MyClass(int x, int y) : _x(x), _y(y) {}
    ~MyClass() {}

    double x() const { return _x; }
    double y() const { return _y; }

private:
    int _x, _y;
};

bool operator<(const MyClass& lhs, const MyClass& rhs) {
    return (lhs.x() < rhs.x()) || (!(rhs.x() < lhs.x()) && (lhs.y() < rhs.y()));
}

Or, define a comparison functor (this is probably the recommended way):

class MyClass{
public:
    MyClass(int x, int y) : _x(x), _y(y) {}
    ~MyClass() {}

    double x() const { return _x; }
    double y() const { return _y; }

private:
    int _x, _y;
};

struct MyComparator {
    // use it like std::map<MyClass, int, MyComparator>
    bool operator()(const MyClass& lhs, const MyClass& rhs) const {
        return (lhs.x() < rhs.x()) || (!(rhs.x() < lhs.x()) && (lhs.y() < rhs.y()));
    }
};

Note that std::map decides two keys are equal if (!(key1 < key2) && !(key2 < key1)).

Articles

Loop over all histograms in pyROOT

In howto on Jun 19, 2016 by theoryl Tagged: , , ,

Here’s a code snippet to loop over all histograms (and get their names) in pyROOT:

from ROOT import TFile

fname = 'histos.root'
hnames = []

tfile = TFile.Open(fname)
for key in tfile.GetListOfKeys():
    h = key.ReadObj()
    if h.ClassName() == 'TH1F' or h.ClassName() == 'TH2F':
        hnames.append(h.GetName())

Articles

Ubuntu wakes up immediately after suspend

In howto on Jun 18, 2016 by theoryl Tagged: , , ,

Sometimes when you try to suspend the Ubuntu operating system, it wakes up immediately. This is often because certain devices are in “awake” state. To fix it, first you need to find out which device it is. Do:

acpitool -w

You might see output like the following:

   Device	S-state	  Status   Sysfs node
  ---------------------------------------
  1. P0P1	  S4	*disabled
  2. GLAN	  S4	*disabled
  3. XHC	  S4	*enabled

To disable ‘XHC’, do:

echo 'XHC' > /proc/acpi/wakeup

with superuser privilege.

Typical suspects are USB devices. To find out the USB devices in your system, do:

lspci | grep USB