*NIX Tricks

[python] Fit a curve through a given set of data points

Posted in python by kousik on April 24, 2012

#!/usr/bin/env python

# import the necessary modules
import numpy as np
from scipy.optimize import curve_fit

# Define the fitting function
def func(x, a, b, c):
  return a*np.exp(-b*x) + c

# Generate x and y values which the curve will be fitted to
# (In practical cases, these should be read in)
x = np.linspace(0,4,50)
y = func(x, 2.5, 1.3, 0.5)
yn = y + 0.2*np.random.normal(size=len(x))

# The actual fitting part
# popt = the fitted parameters as a tuple, namely (a,b,c)
# pconv = The estimated covariance of popt.
#        The diagonals provide the variance of the parameter estimate.
popt, pcov = curve_fit(func, x, yn)

Source: Scipy

Tagged with: , ,

[f77] Concatenate a string with another string or an integer in fortran 77

Posted in fortran, programming by kousik on March 23, 2011

Let’s assume that we have defined two strings as

str1 = 'abc'

str2 = 'pqr'

and an  integer

integ = 25

(Make sure str1 and str2, and integ are defined as character strings, and integer, respectively)

1. Concatenate str1 and str2 into str3 (make sure you define str3 first as a character string)

str3 = str1//str2

2. Concatenate str1 and integ into str3 (again make sure str3 is defined as a character string)

step a: convert integer to a string first (make sure str4 is defined as a character string and not initialized, i.e. it’s so far an empty string)

write(str4,'(I5)') integ

step b: concatenate str1 and str4 into str3

str3 = str1//str4

[fortran] Create a fortran 77 library and link it

Posted in fortran, programming by kousik on December 23, 2009

If you have main.f and as the main fortran 77 program and it calls upon subroutines in other files (say, files 1.f, 2.f,…. 7.f), you can compile as (we’ll use GNU Fortran compiler as an example)

$ gfortran main.f [1-7].f

But if you change only one of them (say main.f) and do not touch others at all, you can make a library out of the unchanging ones and link to the library during compilation:

First, create the library:

$ rm -f *.o
$ gfortran -c [1-7].f
$ ar -rcs libmylib.a [1-7].o

This will create the library libmylib. You may put it in convenient location (might I suggest /usr/local/lib/ directory?). You can list out the object files by using:
$ ar t libmylib.a

Next while compiling main.f, you can link to the library in following three ways:
$ gfortran main.f /path_to_the_library/libmylib.a -o executable
or
$ gfortran main.f -L/path_to_the_library/ -lmylib -o executable
or if the library is in a “well-define” library search path, e.g. /usr/local/lib, then
$ gfortran main.f -lmylib

Reference: Ubuntu forum.

[bash] Conditional operators in BASH

Posted in bash, linux, programming, unix, zsh by kousik on October 8, 2009
-a file
True if file exists.
-b file
True if file exists and is a block special file.
-c file
True if file exists and is a character special file.
-d file
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and its set-group-id bit is set.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its “sticky” bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-t fd
True if file descriptor fd is open and refers to a terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-O file
True if file exists and is owned by the effective user id.
-G file
True if file exists and is owned by the effective group id.
-L file
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N file
True if file exists and has been modified since it was last read.
file1 -nt file2
True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
file1 -ot file2
True if file1 is older than file2, or if file2 exists and file1 does not.
file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers.
-o optname
True if shell option optname is enabled. The list of options appears in the description of the -o option to the set builtin (see The Set Builtin).
-z string
True if the length of string is zero.
-n string
string
True if the length of string is non-zero.
string1 == string2
True if the strings are equal. ‘=’ may be used in place of ‘==’ for strict posix compliance.
string1 != string2
True if the strings are not equal.
string1 < string2
True if string1 sorts before string2 lexicographically in the current locale.
string1 > string2
True if string1 sorts after string2 lexicographically in the current locale.
arg1 OP arg2
OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.

Reference: Copied directly from here.

Follow

Get every new post delivered to your Inbox.