Nutils is a MIT licensed package for Finite Elements applications.

Here you can find some examples how to use it

http://www.nutils.org/

Nutils by example

# Python: Trial and error

## Friday, July 8, 2016

### Nutils: a python based finite element package

## Monday, June 20, 2016

### Reading lines till eof from standard input

If you want to read lines from the standard input until End-Of-File (eof), you
can use the sys.stdin, for example:

```
#!/usr/bin/env python3
import sys
for line in sys.stdin:
print(line,end="",flush=True)
```

### Printing without newline

Print automatically adds a new line after printing its input variables and/or constants.
To avoid this new line in Python2.6+ you can add a colon "," (without quotes) after the printing command, here is an example
In Python3, you can add the option end="". Example:
Remember that from Python2.6+ you also can use the Python3 print command, adding

```
print 'Hello',
print 'World'
```

```
print('Hello',end="")
print('World')
```

```
from __future__ import print_function
```

## Monday, April 4, 2016

### One Liner swaping of variables using tuples

In Python, it is possible to create a one liner code to swap the value of

two variables, this is how

two variables, this is how

a = 2 b = 4 print "a = ",a,"b = ",b # One liner swap of variable using tuples (a, b) = (b, a) print "a = ",a,"b = ",b # To improve the readability # you can write the tuple # without parenthesis, as a, b = b, a

### Matlab spy plot style in Python (sparse pattern)

The first time I used the command spy to show the sparse pattern of a sparse matrix, I wasn't satisfied with the result,

as a person who are used to Matlab plots. In this figure isn't easy to see the sparse pattern of the the matrix. After reading about the options available in the spy command, I found the option

This is the code,

as a person who are used to Matlab plots. In this figure isn't easy to see the sparse pattern of the the matrix. After reading about the options available in the spy command, I found the option

*markersize*. Changing the value of this parameter we can obtain a more familiar plot.This is the code,

```
#!/usr/bin/env python
import scipy.sparse as sparse
import matplotlib.pyplot as plt
import scipy.io as io
import numpy as np
#Downloading the matrix from SPARSEKIT
problem = "SPARSKIT/drivcav/e05r0200"
mm = np.lib._datasource.Repository('ftp://math.nist.gov/pub/MatrixMarket2/')
f = mm.open('%s.mtx.gz' % problem)
A = io.mmread(f).tocsr()
f.close()
#Default Ploting
plt.spy(A)
plt.show()
#Closer to Matlab's result
plt.spy(A,markersize=1.0)
plt.show()
```

Labels:
MatlabPython,
matplotlib,
plot,
sparse matrix,
spy

### Example codes in github

You can find all the code published in this blog in github, please visit https://github.com/astudillor/codes_python_blog. Feel free to use and modify this code.

## Thursday, March 31, 2016

### Checking the type of a variable or object

Recently, I came across to the problem of writing a function that behaves differently according the type of its single input parameter. Due the fact that Python is a dynamically-typed language, this problem does not look too easy to me at first glance. But fortunately, Python provides the build- in command isinstance. This command receives two parameters, the first one is a object and the second is a class or tuple of classes. Then return true is the object is an instance of the class or of the classes in the tuple. Let us exemplify the use of this function with a routine that print the type of its input parameter,

def printType(x): if isinstance(x,(int,long,float,complex)): print "It's a number" return if isinstance(x, str): print "It's a string" return if isinstance(x,list): print "It's a list" return if isinstance(x,dict): print "It's a dictionary" return if isinstance(x,tuple): print "It's a tuple" return

Subscribe to:
Posts (Atom)