During October (2017) I will write a program per day for some well-known
numerical methods in both Python and Julia. It is intended to be an exercise
then don't expect the code to be good enough for real use. Also,
I should mention that I have almost no experience with Julia, so it
probably won't be idiomatic Julia but more Python-like Julia.
Regula falsi
The second method to be considered is the
false position method,
or regula falsi. This method is used to solve the equation \(f(x) = 0\)
for \(x\) real, and \(f\) continuous. It starts with an interval
\([a,b]\), where \(f(a)\) and \(f(b)\) should have opposite signs.
The method is similar to bisection method but instead of halving the original
interval it takes as a new point of the interval the intercept of the line
that connect the function evaluated at the two extreme points. Then, the new
point is computed from
\begin{equation*}
c = \frac{a f(b) - b f(a)}{f(b) - f(a)}
\end{equation*}
As in bisection method, we keep the interval where the solution
appears, i.e., the sign of the function changes.
We will use the function \(f(x) = \cos(x) - x\) to test the codes,
and the initial interval is \([0.5, \pi/4]\).
Following are the codes.
Python
from__future__importdivision,print_functionfromnumpyimportabs,cos,pidefregula_falsi(fun,a,b,niter=50,ftol=1e-12,verbose=False):iffun(a)*fun(b)>0:c=Nonemsg="The function should have a sign change in the interval."else:forcontinrange(niter):qa=fun(a)qb=fun(b)c=(a*qb-b*qa)/(qb-qa)qc=fun(c)ifverbose:print("n: {}, c: {}".format(cont,c))msg="Maximum number of iterations reached."ifabs(qc)<ftol:msg="Root found with desired accuracy."breakelifqa*qc<0:b=celifqb*qc<0:a=creturnc,msgdeffun(x):returncos(x)-xprint(regula_falsi(fun,0.5,0.25*pi))
Julia
functionregula_falsi(fun,a,b,niter=50,ftol=1e-12,verbose=false)iffun(a)*fun(b)>0c=nothingmsg="The function should have a sign change in the interval."elseforcont=1:niterqa=fun(a)qb=fun(b)c=(a*qb-b*qa)/(qb-qa)qc=fun(c)ifverboseprintln("n: $(cont), c: $(c)")endifabs(fun(c))<ftolmsg="Root found with desired accuracy."breakelseifqa*qc<0b=celseifqb*qc<0a=cendmsg="Maximum number of iterations reached."endendreturnc,msgendfunctionfun(x)returncos(x)-xendprintln(regula_falsi(fun,0.5,0.25*pi))
Comparison
Regarding number of lines we have: 29 in Python and 32 in Julia. The comparison
in execution time is done with %timeit magic command in IPython and
@benchmark in Julia.
For the next month I will write a program per day for some well-known numerical
methods in both Python and Julia. It is intended to be an exercise, then
don't expect the code to be good enough for real use. Also, I should mention
that I have almost no experience with Julia, so it probably won't be
idiomatic Julia but more Python-like Julia.
Day 1: Bisection method
The first method to be considered is the
bisection method. This
method is used to solve the equation \(f(x) = 0\) for \(x\) real,
and \(f\) continuous. It starts with an interval \([a,b]\), where
\(f(a)\) and \(f(b)\) should have opposite signs. The method then
proceeds by halving the interval and selecting the one where the solution
appears, i.e., the sign of the function changes.
We will use the function \(f(x) = \cos(x) - x^2\) to test the codes,
and the initial interval is [0, 1].
The following are the codes:
Python
from__future__importdivision,print_functionfromnumpyimportlog2,ceil,abs,cosdefbisection(fun,a,b,xtol=1e-6,ftol=1e-12):iffun(a)*fun(b)>0:c=Nonemsg="The function should have a sign change in the interval."else:nmax=int(ceil(log2((b-a)/xtol)))forcontinrange(nmax):c=0.5*(a+b)ifabs(fun(c))<ftol:msg="Root found with desired accuracy."breakeliffun(a)*fun(c)<0:b=celiffun(b)*fun(c)<0:a=cmsg="Maximum number of iterations reached."returnc,msgdeffun(x):returncos(x)-x**2print(bisection(fun,0.0,1.0))
With result
(0.824131965637207,'Maximum number of iterations reached.')
Julia
functionbisection(fun,a,b,xtol=1e-6,ftol=1e-12)iffun(a)*fun(b)>0c=nothingmsg="The function should have a sign change in the interval."elsenmax=ceil(log2((b-a)/xtol))forcont=1:nmaxc=0.5*(a+b)ifabs(fun(c))<ftolmsg="Root found with desired accuracy."breakelseiffun(a)*fun(c)<0b=celseiffun(b)*fun(c)<0a=cendmsg="Maximum number of iterations reached."endendreturnc,msgendfunctionfun(x)returncos(x)-x^2endprintln(bisection(fun,0.0,1.0))
With result
(0.824131965637207,"Maximum number of iterations reached.")
In this case, both codes are really close to each other. The Python code
has 25 lines, while the Julia one has 27. As expected, the results given by
them are the same.
Edit (2017-10-02)
As suggested by Edward Villegas, I decided to compare execution times.
I used %timeit for IPython and @benchmark (from BenchmarkTools)
for Julia.
In this post, we describe the solution of the Schrödinger equation
using the Ritz method and
Hermite functions
basis. This basis seems to be a good choice for the 1D Schrödinger equation
since its an orthogonal basis over \((-\infty, \infty)\).
being \(\psi\) the wave function, \(V(x)\) the potential and
\(E\) the energy. This variational formulation is equivalent to the
time-independent Schrödinger equation, and \(E\) works as a Lagrange
multiplier to enforce that the probability over the whole domain is 1.
We can expand the wave function in an orthonormal basis, namely
where \(u_n(x) \equiv \mu_n H_n(x) e^{-x^2/2}\) is a normalized
Hermite function, \(\mu_n\) is the inverse of magnitude of the
\(n\)-th Hermite polynomial
The last integral can be computed using
Gauss-Hermite quadrature.
And we will need more Gauss points if we want to integrate higher-order
polynomials. This method would work fine for functions that can be
approximated by polynomials.
Examples
A Python implementation of this method is presented in
this repo.
We can format plots easily using the style package
in matplotlib all. The main idea is to create a file with some of the
parameters that want to be defined (that can also be accessed through
rcParams).
This post is not a tutorial on how to use those, for that you can check
the style sheet reference.
Here, I just want to play with some of these parameters to create three
different styles. The first two examples present the style of an (infamous by
some) software, that is probably used for most people as their visualization
platform, while the third one is just a clean style. All the files used here
can be download
here.
For all the examples below the following imports are done:
In the second example we want to reproduce the offspring of the style
in the first example. This is definitely an improvement over the previous
style, but it is not perfect.
In this post, we find the Hermite interpolation functions for the
domain [-1, 1]. And then, we use it for a pieciwise interpolation. Notice
that this interpolation has \(C^1\) continuity compared to the
\(C^0\) continuity that is common in Lagrange interpolation.
To compute the polynomials explicitly we use sympy.
We want to find a set of basis function that satisfy the following
We can see that the previous matrix is a confluent Vandermonder matrix [1].
It is similar to a Vandermonde matrix for the first \(n\) nodes
and the derivatives of each row for the following ones.
The coefficients for our polynomials are given by the inverse of this matrix.
In this post I want to talk about some tools (or something like that)
that are useful in the day-to-day research, but usually not so popular
for being somewhat tangential to the area specific in which each one works.
Scripting
A script is a
usually simple program that performs a series of tasks.
If there is a task that must be done more than ... five times (the number varies
according to the patience), then I think it's something we can ask the
computer to do for us. In other words: we can automate that work. Some tasks
that can be a good alternative to automate are: rename 100 files,
convert a file from one format to another (e.g. STL to OBJ), read
387 files with information on the climate and graph its evolution
temporary (minimum, maximum and average temperature). These tasks can
be easy to do by hand, but for the amount of work that
they involve are tedious.
The first thing to do is to get a scripting language. Some options are
Python,
Bash,
Julia,
Matlab/Octave,
Scilab. Allowing myself to follow my bias,
I would recommend to use Python.
Graphics and schematics
A picture is worth a thousand words, or so the saying goes.
Personally, it seems absolutely true to me and I try to do
scribbles to better understand something or explain it better.
The first thing I would like to mention is the difference between images
bitmap (or raster) and vector images.
Bitmap image: it's an image
which is represented by an array (or rectangular grid) of pixels. In other
words, the color information that there are in each point of the image. The
most popular formats store the compressed information. For high contrast
graphics (such as schematics or diagrams) the best format is PNG. If you have
an animation, GIF would be preferable. And in the case of photographs it is
better to use JPG.
Vector image:
is an image that is made up of geometric entities. In this case, the
stored information is not point-to-point but the construction of
the shapes that constitute it. For this reason, these images don't
pixelate because the information you have is how to build it.
This type of images is the best options for schematics and diagrams,
since the only stored information are the strokes and text added to
them (see Figure 1). The de facto standard for this type
of images is PDF —it is the one I usually include in my documents
\(\LaTeX\), although there is a way to embed SVG in \(\LaTeX\)
But it's something I haven't yet explored. Although PDF is the
standard, the preferred format is SVG (Scalable Vector
Graphics)
which is a standard across the internet and most
modern browsers allow viewing.
Recapping, we should use JPG images for photographs and SVG
for schematics/diagrams. Another attribute that may be useful is the
Layer management, SVG allows this ... and for raster formats we have the
option to use TIFF.
Regarding software to generate/edit this type of images I must
say that there are a large number of programs that allow exporting to
these formats: Python/Matplotlib, Matlab, Inkscape, Adobe
Illustrator, GIMP, Photoshop, LibreOffice. If the graph is generated
from a calculation or a data series I use Matplotlib. If
instead, we want to make a schematic or my tool of choice
is Inkscape. This
program is intended to be a free alternative to programs like Adobe
Illustrator —and it does achieve it. Obviously, you could use
Illustrator or Corel Draw for this task. If the only use would be to make
Technical schematics, I think it would be a waste.
I suppose that to some it would seem a bit trivial to speak of "notetaking"
and much more coming from someone who didn't have notebooks in
high school, but since I'm a bit stubborn I think I'll still write a
little about this. The first thing I would like to mention is that I remember
people talking to me about this at school, but there was never anything
formal regarding developing these skills. Surfing the web,
there is a lot of information. Even the Wikipedia article on
English is interesting.
There is nothing better to write than to have a good pen and paper with a good
grammage, that's why I still use a notebook where I keep track of
what I do in my research and take notes. However, this scheme is quite linear
and leaves out more contemporary options. That is, why settle for a document
in this time of hyper-documents? The advantages of taking notes
digitally jump to the eye, in a hyper-document you can have
links, embed images, video and sound.
Regarding tools, I include a short list here:
Evernote is probably the most popular tool
for taking notes. It's cross-platform, Freemium (free basic and paid
advanced functionality), and has many options. I use it, but not much in my
research.
Zim it is an offline wiki. Has great
number of options like calendar, equations with \(\LaTeX\), images ...
anyway. The but that I find is that no I have managed to configure the
equations in Windows (and in my office I must use Windows :-/).
Docear this is a tool thought,
mainly, to handle bibliography. However, it allows to take
notes and, in general, handle the information of the investigation. The
more (or less) appealing feature is that it works around
mental maps.
Zotero It is also a tool to handle bibliography,
although it allows to handle some note taking (at least around the
bibliography).
Mendeley it is very similar to the previous one,
although with more functionalities. The biggest but that I find it
is that in 2013 it was bought by Elsevier.
Regarding bibliography management I would also like to mention
EndNote which is the program with the longest
trajectory and JabRef which is the one
that I have used the longest. Some interesting references
comparing bibliography handlers are::
[A][B][C].
Graphic reconstruction
It is common to find information presented in the form of graphics.
It is also common that we want to have the numerical data of these
graphs to be able to compare them with ours. To know if
our measurements/simulations/methods give results similar to others
presented in the literature. We could use powerful image processing software,
or other more modest ones designed specifically for this purpose.
Figure 2. Original graphic.
Figure 3. Graphic processed in Engauge Digitizer. Some points were
selected (automatically) to obtain their coordinates.
Digitizer of XY chart
this is a plugin for Libreoffice/OpenOffice and it exports the result
to the current spreadsheet, it is simple and easy to use.
Engauge Digitizer, is the one
I normally use when I need to do this task (see Figures above). It is
open source (and free) and has a fair amount of options
to make the task easier.
Plot Digitizer I don't have
a lot of information about this one (since I have never used it), except that
it is written in Java.
ImageJ this is a (complete) program for
image processing that is written in Java. I have not used it
for this task on a regular basis, but could be used for it.
Scientific visualization
Scientific visualization
is in charge of generating graphs that allow visualizing "scientific data"
to facilitate the understanding behind the data. For this work, many of us have
used scripting languages such as Matlab/Octave, Scilab or Python (with
Matplotlib or Mayavi). However, as visualization is about something visual
—what else?—, it is good have a tool that allows you to generate and change
graphics interactively, although we must always automate as much
amount of work possible (laziness has always been one of the largest mobiles of
humanity, you have to accept it).
MayaVi, this is a
program written in Python that uses VTK. It is very versatile and the great
advantage it has is that it can be used within Python
scripts.
Paraview, this program is also
based on VTK and allows to parallelize the work (for
multi-core computers and clusters). Below I include a
video generated in Paraview to show its capabilities.
Visit,
this program is also VTK based, I have never used it but
I wanted to include it because people say it can be more intuitive than
Paraview.
Tecplot, this program is very popular
at Purdue. I think it was initially intended for CFD, but it has been
much expanded. Regarding 3D graphics, it does not seem better than
ParaView, however, the 2D graphics capabilities (XY graphics, and
others) make it attractive.
Scavis, this is written in Java. I didn't
know it until I started writing this post but it caught my eye and I wante
to put it on the list. Something that cught my attention is that it allows
scripting in several languages: Java, Python, Ruby, BeanShell, and
Matlab/Octave.
Origin, I have never used it but I didn't
want to leave it out because I've always heard great things about it
(probably comparing it with Excel ... but I can't comment).
Version control is the
management of changes in documents, source code and other types of
information. This can be done manually, but it is easy to make mistakes
or replace the version of a code easily, and for this it is
advisable to use software that facilitates the work. The idea is to
have a place (repository) where versions and changes are stored, and keep
track of them. In this way you can revert to a previous version of documents
and multiple people can work together. There are two paradigms (or
architectures) for version control: centralized and distributed. In the first
there is a centralized repository where you find all the information. In
distributed architectures each user has a copy of the repository. Personally I
have only used Git, which falls under the distributed
category and is one of the most popular version contorl software
at the moment; it is used by companies like Google, Facebook and Netflix.
An example can be seen in this
repository, with the
undergrad thesis document of Santiago Echeverri, which
I had the opportunity to advise. We edited this document
together while he was in Medellin and I was in
USA. The document was made in the markup language
\(\LaTeX\).
In addition to having control over the versions and being able to access
previous versions, it is useful to be able to store the information in a
accesible location from anywhere in the world with a connection to
Internet. This can be achieved with your own server, obviously, or
also through an external provider. Two projects that are very
popular for hosting repositories are
(comparison between Github and
BitBucket):
Github is the most popular at the moment.
It allows to have projects with an unlimited number of collaborators.
To have a private repository it is necessary to pay.
BitBucket has the main advantage that
allows you to have private repositories without the need to pay. It is only
free for projects with 5 or fewer collaborators (or for
academic projects).
Mirando en internet no encontré una versión en español de la fábula de
Esopo "La zorra y las uvas" que me gustara (o que al menos rimara :P).
De hecho, el artículo de Wikipedia en
español es
bastante más malo que su equivalente en
inglés. Por
esto, y por petición de una amiga, hice mi versión con rima asonante. Y
reza así
Last year baby Jesus gave me a Samsung Galaxy Note
10.1
(model 2012). This tablet is designed for taking notes, as it brings
an S-Pen (made by Wacom, which I think makes the
technology
for most commercial tablets) and various applications
designed for it. Such as: S Note, PhotoShop Touch, Crayon
Physics (you have to play it is very
fun).
So I decided to make an experiment taking notes in class with my
tablet completely in the two subjects I studied in the spring 2014 term.
In Optimization I used the pencil that came with the
tablet and for Molecular Physics I used a Bamboo
Stylus
which is supposed to be designed specifically for this tablet. I took
notes with the default software (S Note) and converted to PDF
(the notes were made entirely with the software):
Although the touch sensation of the Bamboo Stylus is much more pleasant,
the performance of the S Pen is much better. In both cases, the feeling is
very different from using paper and pencil (pen/fountain pen), largely
because friction is very little in comparison (something that is
better with conductive pencils). The user experience of the
pencil varies greatly from one application to another, resulting in good
expectations. As for taking notes: it is highly recommended to take
short notes (like meetings); not so much for taking long notes (like
class notes), unless you want to have a digital copy
easily.
Apps
The technology behind the tablet is the most important part. Simply pu, the pen
has an antenna and the tablet a grid of antennas that are tuned to the same
frequency (531 kHz, in
this link
they explain how it works a little better). However, it is also
important to consider the software that is used to take notes:
It has shape and equations recognition. Equations should be simple,
don't expect to take notes of Quantum Mechanics or Continuum Mechanics
using this tool.
It allow to insert images and record sound within the app.
It is open source, although if you get it through "Google Store" it
has a cost.
The algorithm for path recognition is better than the others (and
it is adjustable).
It is still in a preliminary stage.
Sadly, there isn't a version of Paper
(by FiftyThree) on Android, and, although there is a version of Bamboo
Paper (by Wacom)
for Android it is not compatible with the tablet.
In conclusion
Replacing "traditional" note taking with "digital" notes
It may be a bit rushed, but it is doable. The application that
comes by default (S Note) allows to do the job "out of the
box", but it could be better. The applications that I find the most
Promising are: Note Anytime and Quill.
Cuando estaba en el colegio, en ocasiones mencionaban las palabras terminadas
en "ar", "er" o "ir" —iguales al final de nuestros verbos en infinitivo— que no
eran verbos. Eran pocos ejemplos los que mencionaban, así que algunos años
después me dio el arrebato de hacer una
lista… y hela aquí:
AR
AR
AR
ER
IR
agar
dispar
pajar
alfiler
cachemir
ajuar
ejemplar
paladar
alquiler
casimir
alar
escolar
palatar
amater
elixir
albañar
espectacular
palomar
anteayer
emir
albar
estelar
papilar
antier
faquir
alfar
extracurricular
par
atelier
kefir
alizar
familiar
paramilitar
ayer
menhir
almiar
fular
particular
bachiller
nadir
almimbar
globular
peculiar
bereber
porvenir
alminar
hangar
pendular
brigadier
sir
altar
hogar
peninsular
canciller
souvenir
alveolar
ijar
perpendicular
cualquier
visir
antealtar
impar
pilar
doquier
zafir
antisolar
insular
pinar
dossier
ar
intercelular
planar
enser
auricular
intraocular
polar
ester
avatar
jaguar
policelular
menester
axilar
juglar
pulgar
mercader
azahar
lagar
pulmonar
micer
azar
lanar
quasar
neceser
bacillar
lar
radar
palier
bar
limonar
radicular
placer
bazar
llar
rectangular
plumier
bicelular
lobular
reticular
premier
bienestar
lodazar
samovar
primer
bifilar
lugar
secular
rosicler
biliar
lunar
seglar
salmer
billar
malar
semicircular
somier
binocular
maleolar
semilunar
sumiller
bipolar
malvar
similar
taller
broncopulmonar
mandibular
solar
tercer
bulevar
manglar
subcelular
veguer
calamar
manillar
supraclavicular
calcañar
manjar
supralunar
capilar
mar
telar
capsular
maxilar
testicular
caviar
melocotonar
tisular
celular
melonar
trifilar
centenar
membrillar
trufar
ciliar
miliar
tubular
clavicular
militar
tular
collar
millar
ultramar
consular
molar
uvular
coplanar
mollar
vascular
crepuscular
muladar
vehicular
corpuscular
monocelular
vermicular
cuadrangular
mular
vesicular
cular
muscular
vulgar
curricular
nuclear
yugular
dactilar
ocular
zar
dinar
olivar
Si tienes alguna sugerencia de palabra que haga falta la puedes dejar en los
comentarios.
Última actualización: Octubre 21, 2022.
Descargar la lista en formato CSV o en formato
XLS.
A friend once asked me about which of two methods was easier to
teleport people. One consisted of "deconfiguring" and "reconfiguring"
the person (as information, similar to Start Trek
and the other in "creating a shortcut" between two places in space-time to pass
to the person. Obviously, in both cases assuming that such things can be
done.
Regarding the creation of the shortcut in space-time, I think I have not
studied the equations of General Relativity to be able to do the math,
although perhaps in Física Pasión
give us these calculations.
The first of the alternatives consists of, basically, the representation of a
person as information and its "deconfiguration" processing, transmission and
processing of "reconfiguration". Since as humans we are made, mostly,
of water we will assume a person of water for the calculations. We will also take
as reference mass \(70\, \mbox {kg}\).
Some data that we will use are:
The molar mass of water is \(M_{H_2O}=18,01528\ \mbox{g/mol}\);
Avogradro number is \(N_A=6,022 \times 10^{23}/\mbox{mol}\).
Then, we have that the number of moles in a person is
Water has 12 vibrational and 6 translational modes, and furthermore 40 quantum
electronic numbers. This add up to 58 degrees of freedom for each molecule.
Thus, the total number of degrees of freedom is
In 2011 there was a transmission record
of 186 Gb/s, considering this rate the time it would take to transmit
all data (assuming data represented as 32 bits real numbers)
would take \(2,3 \times 10^{16}\ \mbox {s}\) or, equivalently,
\(1.70 \times 10^{10}\) years (the estimated age of the universe is
\(1.37 \times 10^{10}\) years).
If we wanted this to happen really fast, say in 1 millisecond, the transmission
rate should be \(2.3 \times 10^{19}\) faster than the record they reached
at CERN… something that is inconceivable for us today.