[python] Fit a curve through a given set of data points
#!/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
[f77] Concatenate a string with another string or an integer in fortran 77
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
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
-afile- True if file exists.
-bfile- True if file exists and is a block special file.
-cfile- True if file exists and is a character special file.
-dfile- True if file exists and is a directory.
-efile- True if file exists.
-ffile- True if file exists and is a regular file.
-gfile- True if file exists and its set-group-id bit is set.
-hfile- True if file exists and is a symbolic link.
-kfile- True if file exists and its “sticky” bit is set.
-pfile- True if file exists and is a named pipe (FIFO).
-rfile- True if file exists and is readable.
-sfile- True if file exists and has a size greater than zero.
-tfd- True if file descriptor fd is open and refers to a terminal.
-ufile- True if file exists and its set-user-id bit is set.
-wfile- True if file exists and is writable.
-xfile- True if file exists and is executable.
-Ofile- True if file exists and is owned by the effective user id.
-Gfile- True if file exists and is owned by the effective group id.
-Lfile- True if file exists and is a symbolic link.
-Sfile- True if file exists and is a socket.
-Nfile- True if file exists and has been modified since it was last read.
- file1
-ntfile2 - True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
- file1
-otfile2 - True if file1 is older than file2, or if file2 exists and file1 does not.
- file1
-effile2 - True if file1 and file2 refer to the same device and inode numbers.
-ooptname- True if shell option optname is enabled. The list of options appears in the description of the -o option to the
setbuiltin (see The Set Builtin). -zstring- True if the length of string is zero.
-nstring- 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
OParg2 OPis 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.

leave a comment