http://alecjacobson.com/art/digital/
http://alecjacobson.com/art/
40 close
July 24th, 201040
July 24th, 2010Real time tester
July 22nd, 2010
I’ve adapted an old applet to function as a real time tester. The idea being that you hear a new graphics algorithm is “real-time” or “interactive” because it runs in only “59ms” per frame, but your internal clock isn’t accurate enough to really know what that feels like. Here you can specify the simulated “solve” time and drag around the bezier curve as if hard-core math is going on between frames.
Extracting entries at multiple indices from std::vectors in c++, from zipped matrices
July 20th, 2010As a naturally follow up to my last post, I have extended my C++ implementation of matlab like array indexing to work for “zipped matrices”. I often store matrices zipped into std::vectors, column-wise. Now I can access them like how I do in matlab. Suppose in matlab I had:
A = [10,11,12;13,14,15];
You could then suck out a re-ordered block of rows and columns of the matrix by calling:
B = A([1],[3,2]);
Revealing:
B =
12 11
This is especially convenient for chopping up and reordering rows and columns in linear systems.
I can do this same sort of indexing with C++, the caveat being that I have to pass the number of columns as a third argument. For now, I think I would rather do this than have it be a field of the SmartMatrix.
#include <vector>
#include <stdio.h>
template<typename T>
class SmartMatrix: public std::vector<T>{
public:
// act like operator[]
T operator()(
size_t _row,
size_t _col,
size_t number_of_columns){
return (*this)[_row*number_of_columns+_col];
}
// act like matlab operator()
SmartMatrix<T> operator()(
std::vector<size_t>& row_positions,
std::vector<size_t>& column_positions,
size_t number_of_columns){
SmartMatrix<T> sub;
sub.resize(row_positions.size()*column_positions.size());
size_t sub_i = 0;
for(
std::vector<size_t>::iterator rpit = row_positions.begin();
rpit != row_positions.end();
rpit++,sub_i++){
size_t sub_j = 0;
for(
std::vector<size_t>::iterator cpit = column_positions.begin();
cpit != column_positions.end();
cpit++,sub_j++){
sub[sub_i*column_positions.size() + sub_j] =
(*this)[(*rpit)*number_of_columns + (*cpit)];
}
}
return sub;
}
};
int main(){
SmartMatrix<int> A;
size_t cols_in_A = 3;
size_t rows_in_A = 2;
A.push_back(10);
A.push_back(11);
A.push_back(12);
A.push_back(13);
A.push_back(14);
A.push_back(15);
A.push_back(16);
printf("A=\n");
for(int i=0;i<rows_in_A;i++){
for(int j=0;j<cols_in_A;j++)
printf("%d ",A[i*cols_in_A+j]);
printf("\n");
}
printf("\nA[%d,%d]=%d\n",0,2,A(0,2,cols_in_A));
// make some list of indices
std::vector<size_t> sub_row_indices;
sub_row_indices.push_back(0);
std::vector<size_t> sub_column_indices;
sub_column_indices.push_back(2);
sub_column_indices.push_back(1);
// use operator() to extract entries at those indices
SmartMatrix<int> B = A(sub_row_indices,sub_column_indices,cols_in_A);
printf("\nB = A([ ");
for(int i =0;i<sub_row_indices.size();i++)
printf("%d ",(int)sub_row_indices[i]);
printf("],[ ");
for(int i =0;i<sub_column_indices.size();i++)
printf("%d ",(int)sub_column_indices[i]);
printf("]);\n\n");
printf("B=\n");
for(int i=0;i<sub_row_indices.size();i++){
for(int j=0;j<sub_column_indices.size();j++)
printf("%d ",B[i*sub_column_indices.size()+j]);
printf("\n");
}
return 0;
}
Which should output:
A=
10 11 12
13 14 15
A[0,2]=12
B = A([ 0 ],[ 2 1 ]);
B=
12 11
Extracting entries at multiple indices from std::vectors in c++
July 20th, 2010I’ve been doing some C++ coding, working with matrices. In a previous project it was convenient to think of our systems in block form. This translates easily into matlab’s indexing. So say you have in matlab:
a = [10,11,12,13,14,15];
Then you can extract elements 1,3,5 (remember matlab is one-indexed) by calling
b = a([1,3,5]);
which reveals
b =
10 12 14
So in C++, I’ve made my first attempt at useful templating to extend the std::vector class to have this feature. It is no where near implemented the full features of matlab’s operator(), but it seems to do this particular feature correctly.
#include <vector>
#include <stdio.h>
template<typename T>
class SmartVector : public std::vector<T>{
public:
// act like operator[]
T operator()(size_t _Pos){
return (*this)[_Pos];
}
// act like matlab operator()
SmartVector<T> operator()(std::vector<size_t>& positions){
SmartVector<T> sub;
sub.resize(positions.size());
size_t sub_i = 0;
for(
std::vector<size_t>::iterator pit = positions.begin();
pit != positions.end();
pit++,sub_i++){
sub[sub_i] = (*this)[*pit];
}
return sub;
}
};
int main(){
// make some vector
SmartVector<int> a;
a.push_back(10);
a.push_back(11);
a.push_back(12);
a.push_back(13);
a.push_back(14);
a.push_back(15);
for(int i =0;i<a.size();i++)
printf("a[%d] = %d\n",i,a[i]);
// make some list of indices
std::vector<size_t> sub_indices;
sub_indices.push_back(0);
sub_indices.push_back(2);
sub_indices.push_back(4);
// use operator() to extract entries at those indices
SmartVector<int> b = a(sub_indices);
printf("\nb = a([ ");
for(int i =0;i<sub_indices.size();i++)
printf("%d ",(int)sub_indices[i]);
printf("]);\n\n");
for(int i =0;i<b.size();i++)
printf("b[%d] = %d\n",i,b[i]);
return 0;
}
Then if you run ./main you should see:
a[0] = 10
a[1] = 11
a[2] = 12
a[3] = 13
a[4] = 14
a[5] = 15
b = a([ 0 2 4 ]);
b[0] = 10
b[1] = 12
b[2] = 14
Boston T map, single image iphone app
July 18th, 2010I made this app to test my new make your own single image iphone app feature. Like all single image apps made with the new feature, this app is fully cached when you add it to your iphone’s homescreen. So that means that you can use it in the subway when you don’t have internet connection.
Academic site
July 18th, 2010I have finally put up a separate academic site: http://cs.nyu.edu/~jacobson. There I’ll list publications, project pages, research interests and various work/school related addresses.

mixed fem google images search
July 18th, 2010I was mindlessly searching on google for “mixed fem”, as in “Mixed Finite Elements Method”, and when I switched to searching images I was surprised by the results:

Textbook covers, heat maps, marijuana and semi-nude wrestling. See for yourself!
self-portrait #2 redux
July 17th, 2010Convert PDF to grayscale and compress
July 16th, 2010Recently I compressed a 25 MB color pdf to a 900 KB grayscale pdf. Here’s how I did it:
- Open the original.pdf in Preview. Select File > Save As… and choose Quartz Filter: Gray Tone. Save it as original_gray.pdf. My 25 MB color image was reduced to 13 MB by this step alone.
Note: You could do this in Acrobat but it’s much harder to find in the UI and actually crashed the program on my pdf. - Open original_gray.pdf in Acrobat. Select File > Save As… and choose Format: Acrobat PDF Files, Optimized. Then click Settings.
Make sure Images is selected on the left.
Change the color Downsample: line to Bicubic Downsampling to 200 ppi for images above 300 ppi
Change the color Compression: line to JPEG, High
Change the grayscale Downsample: line to Bicubic Downsampling to 200 ppi for images above 300 ppi
Change the grayscale Compression: line to JPEG, HighChange the monochromatic Downsample: line to Bicubic Downsampling to 72 ppi for images above 108 ppi
Change the monochromatic Compression: line to CCITT Group 4
Note: You could do this in Preview with Quartz Filter: Reduce File Size, but some of images got their colors inverted.
Before
|
—> |
After
|





