Posts
Using Microsoft Word for Technical Documents
Microsoft Word is not the best tool for doing technical writing. However, sometimes we are required to use Word because we need to collaborate with others who want to use Word. Right now, I am using Word 2007 on Windows XP to write several mathematical papers. In general, it is a big improvement from previous versions. The new integrated equation editor is outstanding–except for the major bug I’ll discuss below. Here is a brief “FAQ” you will want to read if you are using Word for technical writing. **Q:**How do I enter multi-line equations in Word 2007?**A:Press Shift-Enter where you want a line break to appearQ:**How do I prevent a page break from splitting a table into two parts?**A:**Select the table. On the Home tab, click the little box in the lower-right corner of the Paragraph box. On the paragraph dialog, choose the “Lines and Page Breaks” tab. Check the box for “Keep Lines Together.”**Q:**How do I automatically number headings in Word 2007? For example: Section 1, Subsection 1.1, 1.2, 1.3, Section 2, etc.**A:**It’s not obvious. See this page from dummies.com about number headings.**Q:**In Word 2007, why do equations sometimes appear as blank spaces or question marks when I print or save my document as a PDF file?**A.**This occurs when Word is installed on Windows XP Pro. See the following Microsoft tech support item to find out how to install missing scripts: The characters in an equation are not printed… You may also have an outdated printer driver: Microsoft Support Item 920228**Q:**Why do equations created in Word 2007 disappear when I open the document in Word 2008 for Mac?A. Word for Mac does not support equations written in the new Word 2007 equation editor. Unfortunately, neither does PowerPoint 2007 on the PC. You can work around this by inserting equations into Word the old-fashioned way: go to the Insert tab, click on Object (found in the “Text” box towards the right side of the tab), and choose “Microsoft Equation 3.0” from the list in the dialog box.
Reading an array from a text file with Fortran 90/95
If you’re used to coding in more modern languages, Fortran I/O can seem a little bizarre. Strings in Fortran are much more difficult to work with, since they are fixed-length rather than null-terminated. The following example illustrates a simple way to read an array of numbers from a text file when the array length is unknown at compile time.
program io_test
real, dimension(:), allocatable :: x
integer :: n
open (unit=99, file='array.txt', status='old', action='read')
read(99, *), n
allocate(x(n))
read(99,*) x
write(*,*) x
endHere is the text file that the array is read from. The integer on the first line is the number of elements to read from the next line.
Managing a pool of MPI processes with Python and Pypar
MPI is a standard for communication between multiple processes in parallel computing. These processes can be running on different cores, CPUs, or entirely different computers in a grid. MPI is a standard, and there are many implementations available (many are open source). The features of MPI can be accessed from Python with the packages pypar and mpi4py. Here I present a Python script that implements a “process pool” design pattern using pypar. I have observed a pattern often enough in my own work that I wrote this framework to avoid reinventing the wheel every time I come across it. This pattern is useful for any embarrassingly parallel problem. This describes a computing task that can be easily accelerated by running multiple parallel processes that do not not need to interact with one another. For example, I have large scientific data sets from several runs of an experiment that need to be analyzed. Since the data from each run can be analyzed independently from the other runs, I can analyze all the data sets at once on a parallel machine. The code below implements a “master-worker” paradigm that requires at least three processes to accelerate the calculation. The first process be comes the master, which does no calculation but hands out tasks to the workers. The rest of the processes are workers, which receive a chunk of work, finish it, return the result to the master process, and then wait for more work.
Boost.python Numpy example
Before recent advances such as Cython, Boost.Python was one of the best ways to integrate C++ code into Python programs. The following C++ source code exposes a C++ function to Python. The function takes a NumPy array as an argument and extracts a C++ integer type.
#include
#include "boost/python/extract.hpp"
#include "boost/python/numeric.hpp"
#include
using namespace boost::python;
// Functions to demonstrate extraction
void setArray(boost::python::numeric::array data) {
// Access a built-in type (an array)
boost::python::numeric::array a = data;
// Need to array elements because their type is unknown
std::cout << "First array item: " << extract(a[0]) << std::endl;
}
// Expose classes and methods to Python
BOOST_PYTHON_MODULE(TestNumPy) {
boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
def("setArray", &setArray);
}
```---
## Comments
### Comment #1 by ARAMBAM JAMES SINGH
[ARAMBAM JAMES SINGH](mailto:<jamesastrick@gmail.com>) - Aug 2, 2016
I get this error : import TestNumpy as tnp ImportError: dynamic module does not define init function (initTestNumpy)
---
#### Comment #2 by James
[James](mailto:<jameastrick@gmail.com>) - Aug 0, 2016
Hi , I get the error "Segmentation fault: 11" when i run the python code after compiling above c++ code #--------------- # import TestNumPy as nt import numpy as np a = np.array(\[10,20,30\]) nt.setArray(a)
---Linking Python and C++ with Boost.python
Introduction to Boost
Boost is a project to create open-source libraries for C++. There are a lot of nice tools available through Boost, one of which is Boost.python. Boost.Python provides seamless interoperability between C++ and Python. I have found it to be an extremely useful tool for scientific programming. Parts of a program that require more flexibility than speed can be written in Python, and critical portions can be coded in C++.
Constrained least-squares fitting with Python
Scipy contains a good least-squares fitting routine, leastsq(), which implements a modified Levenberg-Marquardt algorithm. I just learned that it also has a constrained least-squared routine called fmin_slsqp(). I am using simple upper and lower bound constraints, but it’s also possible to specify more complex functional constraints. What I did not realize, at first, is that fmin_slsqprequires a different type of objective function thanleastsq. leastsqrequires you to write a function that returns a vector of residuals, andleastsqautomatically squares and sums the residuals.fmin_slsqp is actually more flexible, in that it can use any objective function that returns a single scalar value. To implement least-squares curve fitting, your objective function will need to find the residual at each data point, square the values, and sum them up. Hopefully this tip will save you some time. Check out the scipy optimization tutorial for more examples. Here is the original paper by Dieter Kraft which introduces the algorithm used by fmin_slsqp.
A preliminary review of the Lehigh Rendition console
In my last post, I explained why I don’t think the ETC Element is a good replacement for the ETC Express. When I was at LDI 2009, I ran across the Rendition series of consoles from Lehigh Lighting. Based on the literature I picked up, and the extensive demonstration that I received from the Lehigh representative, it looks like the Rendition might be the true heir to the Express. It is targeted at a similar market: small theaters, churches, and schools. The physical layout will be familiar to anyone with theater console experience: 24 or 48 submasters on the left, dual cue playback controls in the center, and hardkeys on the right, with channel faders (48 or 96) along the upper right portion of the desk. The submasters are traditional theater-style subs. Each sub records a fixed look rather than an independent cuelist. Shows can be saved to a flash drive via a USB port.
While the console hardware is finalized, several additional features will be added in an upcoming version of the operation system. The console currently supports one external monitor, but dual monitors will soon be supported. An Ethernet port is already included on the console. In a future software release, it will be possible to add wings with additional submasters or channel faders. The advantage of Ethernet over USB is that a wing can be placed hundreds of feet from the console, and connected using existing Ethernet lines. This feature would allow a wing to to be used as remote focus unit. Using a wireless Ethernet bridge opens up even more possibilities. The main difference between the two models, the 24/48 and 48/96, is the number of hardware faders. The software capabilities are identical. The price of the console depends on how many channels of conventional dimming are “unlocked.” You can choose from 125, 250, or 512 conventional channels. All models support 1024 channels of moving-light control. The two hardware DMX ports each support 512 channels, but two additional universes can be accessed via Ethernet (I’m not sure of the details on how this works). The console seems to support all the basic and advanced cue and effect functions that you’d expect: multiple cue lists, flexible cue timing options, macros, subroutines, and effects. I can’t really comment further about that, since I would need to spend a lot of time with the console to accurately gauge its reliability and ease of use. Moving lights are also supported, with fixture profiles for easy patching and a trackball for focusing. Once again, it looks promising, but the only way to really evaluate these features is to try to use them and see how intuitive the process is. Download the offline editing software and try it for yourself. A more sophisticated console called the Rendition Pro is expected to be available in the second quarter of 2010. The Pro is aimed at more experienced users, and appears to compete with the ETC Eos and Ion. It will run the same software as the Rendition, so many of the features that are expected to be added to the Rendition are actually being developed for the Pro. The layout is similar to the Rendition, with playback masters on the left side, traditional cue playback controls in the lower center, and keys to the lower right. Two LCD displays with encoder wheels and softkeys occupy the space above the keypad, with no hard channel faders. Further, the submasters have LCD displays and additional buttons–probably “Go” and “Pause” buttons to support a cue stack on each sub. From my limited experience, both of these consoles series are worthy of further investigation. They seem to be solidly built and well-engineered. The Lehigh personnel that I spoke to were helpful and friendly, and I appreciate their assistance in answering my questions.
Storing large Numpy arrays on disk: Python Pickle vs. HDF5
In a previous post, I described how Python’s Pickle module is fast and convenient for storing all sorts of data on disk. More recently, I showed how to profile the memory usage of Python code. In recent weeks, I’ve uncovered a serious limitation in the Pickle module when storing large amounts of data: Pickle requires a large amount of memory to save a data structure to disk. Fortunately, there is an open standard called HDF, which defines a binary file format that is designed to efficiently store large scientific data sets. I will demonstrate both approaches, and profile them to see how much memory is required. I am writing the HDF file using the PyTables interface. Here’s the little test program I’ve been using: [code language=“Python”]#!/usr/bin/env python from numpy import array
Python Basics: Tutorials and Examples
If you’re new to Python and wondering how to use some of its features, you may find something useful in the following posts:
Storing data with Pickle Tools for Python software development Redirecting standard output from Python Python threads example