Processing & Reactivision - Remove Movie from stage - processing

I'm using Processing and Reactivision to make an interactive table, each symbol plays a video. The problem I have is that when you remove a symbol the video remains and doesn't reset the stage, so when another symbol is added it plays below the video that has already been played. What I basically need to do is, once a symbol is removed the stage resets / clears. Any help would be greatly appreciated. Code below
/*
TUIO processing demo - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten#iua.upf.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// we need to import the TUIO library
// and declare a TuioProcessing client variable
import TUIO.*;
import java.util.*;
import processing.video.*;
TuioProcessing tuioClient;
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 0;
float table_size = 760;
float scale_factor = 1;
PFont font;
PImage img;
Movie theMov;
Movie theMov2;
void setup()
{
//size(screen.width,screen.height);
size(720,480);
noStroke();
fill(0);
img = loadImage("background.jpeg");
theMov = new Movie(this, "gibi.mov");
theMov2 = new Movie(this, "rick.mov");
loop();
frameRate(30);
//noLoop();
font = createFont("Arial", 18);
scale_factor = height/table_size;
// we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods (see below)
tuioClient = new TuioProcessing(this);
}
// within the draw method we retrieve a Vector (List) of TuioObject and TuioCursor (polling)
// from the TuioProcessing client and then loop over both lists to draw the graphical feedback.
void draw()
{
background(247,73,2);
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
image(theMov, 0, 0);
image(theMov2, 0, 0);
Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
pushMatrix();
//translate(tobj.getScreenX(width),tobj.getScreenY(height));
rotate(tobj.getAngle());
rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
popMatrix();
fill(255);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
}
}
// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
if ( tobj.getSymbolID() == 0) {
theMov.play();
}
if ( tobj.getSymbolID() == 5) {
theMov2.play();
}
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
if ( tobj.getSymbolID() == 0) {
theMov.stop();
}
if ( tobj.getSymbolID() == 5) {
theMov2.stop();
}
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
void movieEvent(Movie m) {
m.read();
}
TUIO: missing or wrong 'addTuioCursor(TuioCursor tcur)' method implementation
TUIO:missing or wrong 'removeTuioCursor(TuioCursor tcur)' method implementation
TUIO: missing or wrong 'updateTuioCursor(TuioCursor tcur)' method implementation
add object 0 (3) 0.37709242 0.8826896 0.0
update object 0 (3) 0.37709242 0.8737366 0.0742998 0.2419723 0.0 6.5397916 0.0
update object 0 (3) 0.37709242 0.867628 0.0742998 0.21064281 0.0 -1.080327 0.0
update object 0 (3) 0.37597793 0.86093885 0.0742998 0.19375265 0.0 -0.48257622 0.0
update object 0 (3) 0.37597793 0.8532117 0.0742998 0.23415594 0.0 1.2243422 0.0
update object 0 (3) 0.37896088 0.847547 0.0742998 0.15614879 0.0 -1.9026133 0.0
update object 0 (3) 0.38109976 0.84274954 0.0742998 0.1811261 0.0 0.86128664 0.0
update object 0 (3) 0.37981042 0.838495 0.0742998 0.116989255 0.0 -1.6878119 0.0
update object 0 (3) 0.38065997 0.838495 0.0742998 0.033981323 0.0 -3.3203173 0.0
update object 0 (3) 0.38252023 0.8398745 0.020077623 0.048248682 -0.17978598 0.29723665 -3.7455413
update object 0 (3) 0.38252023 0.8426642 0.020077623 0.09963129 0.0 1.835093 6.4209275
update object 0 (3) 0.38413188 0.8452217 0.020077623 0.10424032 0.0 0.1589322 0.0
update object 0 (3) 0.38413188 0.8452217 0.020077623 0.0 0.0 -4.009243 0.0
update object 0 (3) 0.38325775 0.8440129 0.020077623 0.033149652 0.0 0.73665893 0.0
update object 0 (3) 0.38325775 0.8427188 0.020077623 0.04313787 0.0 0.33294064 0.0
update object 0 (3) 0.38325775 0.8414247 0.020077623 0.05391985 0.0 0.4492492 0.0
update object 0 (3) 0.38222247 0.8414247 0.020077623 0.032352652 0.0 -0.67397493 0.0
update object 0 (3) 0.3830283 0.8414247 0.020077623 0.01714534 0.0 -0.32355985 0.0
update object 0 (3) 0.3830283 0.8424627 0.020077623 0.03992365 0.0 0.8760888 0.0
update object 0 (3) 0.3830283 0.8424627 0.020077623 0.0 0.0 -1.596946 0.0
update object 0 (3) 0.3830283 0.84123003 0.020077623 0.037354052 0.0 1.131941 0.0
update object 0 (3) 0.382105 0.84123003 0.020077623 0.026379995 0.0 -0.31354448 0.0
update object 0 (3) 0.381026 0.83904475 0.020077623 0.081238225 0.0 1.8286079 0.0
update object 0 (3) 0.381026 0.83904475 0.020077623 0.0 0.0 -2.3210924 0.0
update object 0 (3) 0.381026 0.83904475 0.020077623 0.0 0.0 0.0 0.0
update object 0 (3) 0.37948263 0.8377506 0.020077623 0.06294223 0.0 1.9669446 0.0
update object 0 (3) 0.37948263 0.8377506 0.020077623 0.0 0.0 -1.851242 0.0
update object 0 (3) 0.37948263 0.8377506 0.020077623 0.0 0.0 0.0 0.0
update object 0 (3) 0.37759778 0.8357668 0.020077623 0.08048391 0.0 2.367174 0.0
update object 0 (3) 0.37759778 0.8357668 6.280155 0.0 -0.11492891 -2.5151222 -3.5915282
update object 0 (3) 0.3761473 0.8357668 6.280155 0.04395427 0.0 1.3319477 3.4826941
update object 0 (3) 0.3761473 0.8336429 6.280155 0.060682636 0.0 0.47795326 0.0
update object 0 (3) 0.3761473 0.8336429 6.280155 0.0 0.0 -1.8963323 0.0
update object 0 (3) 0.3761473 0.8336429 6.280155 0.0 0.0 0.0 0.0
update object 0 (3) 0.37695312 0.8336429 0.0 0.02441912 0.014613922 0.7399733 0.44284612
update object 0 (3) 0.37695312 0.8336429 6.2801733 0.0 -0.013315989 -0.6783089 -0.77583086
update object 0 (3) 0.37695312 0.83588284 0.0010060358 0.072256215 0.020629212 2.3308456 1.0950066
update object 0 (3) 0.37695312 0.83588284 6.2821803 0.0 -0.010325174 -2.3308456 -0.99852866
update object 0 (3) 0.37695312 0.83588284 0.0080239 0.0 0.04105623 0.0 1.4680401
update object 0 (3) 0.37780267 0.83588284 6.2811527 0.026547907 -0.050016034 0.82962203 -2.846008
update object 0 (3) 0.37780267 0.83588284 0.0030150663 0.0 0.012552409 -0.41481102 0.97763187
update object 0 (3) 0.37780267 0.83447266 0.0030150663 0.040291037 0.0 1.1511725 -0.35864025
update object 0 (3) 0.37780267 0.83447266 0.0030150663 0.0 0.0 -1.2209406 0.0
update object 0 (3) 0.37780267 0.83447266 0.0030150663 0.0 0.0 0.0 0.0
update object 0 (3) 0.37780267 0.83548 0.0030150663 0.031478703 0.0 0.9837094 0.0
update object 0 (3) 0.37780267 0.8365726 0.0030150663 0.036420427 0.0 0.16472414 0.0
update object 0 (3) 0.37780267 0.8365726 0.0030150663 0.0 0.0 -1.0405836 0.0
update object 0 (3) 0.37780267 0.8365726 6.2781296 0.0 -0.03568064 0.0 -0.9911289
update object 0 (3) 0.37780267 0.83493704 6.2781296 0.054518383 0.0 1.8172795 1.1893547
update object 0 (3) 0.37780267 0.8339297 0.0010060358 0.02878053 0.027564982 -0.73536724 0.7875709
update object 0 (3) 0.37780267 0.8339297 6.274067 0.0 -0.051979534 -0.9284042 -2.565952
update object 0 (3) 0.37780267 0.83292246 6.274067 0.028778825 0.0 0.82225215 1.4851296
update object 0 (3) 0.37780267 0.83292246 0.001011122 0.0 0.048852257 -0.87208563 1.4803715
update object 0 (3) 0.37780267 0.83421654 6.27816 0.041744404 -0.030989556 1.3465937 -2.5755424
update object 0 (3) 0.37876967 0.83421654 0.0040691537 0.027628625 0.041355457 -0.40330797 2.0670004
update object 0 (3) 0.37876967 0.83421654 6.2811527 0.0 -0.029428765 -0.83723104 -2.1449766
update object 0 (3) 0.37876967 0.83421654 6.2811527 0.0 0.0 0.0 0.9196489
update object 0 (3) 0.37876967 0.8320927 6.2811527 0.06636977 0.0 2.0740552 0.0
update object 0 (3) 0.37876967 0.8320927 6.2811527 0.0 0.0 -1.8436048 0.0
update object 0 (3) 0.37768522 0.8320927 6.2811527 0.032862026 0.0 0.995819 0.0
update object 0 (3) 0.37768522 0.8320927 6.2811527 0.0 0.0 -1.0954009 0.0
update object 0 (3) 0.37768522 0.8320927 6.2811527 0.0 0.0 0.0 0.0
update object 0 (3) 0.37865222 0.8320927 6.2811527 0.02844123 0.0 0.8365067 0.0
update object 0 (3) 0.37865222 0.83096933 6.2811527 0.025531108 0.0 -0.06613915 0.0
update object 0 (3) 0.37865222 0.8295045 6.2811527 0.056340143 0.0 1.1849629 0.0
update object 0 (3) 0.37865222 0.8276982 6.2811527 0.0410527 0.0 -0.3474419 0.0
update object 0 (3) 0.37865222 0.8276982 6.2811527 0.0 0.0 -1.7105291 0.0
update object 0 (3) 0.37865222 0.8276982 6.2811527 0.0 0.0 0.0 0.0
update object 0 (3) 0.37865222 0.8256836 6.2811527 0.05444804 0.0 1.4715686 0.0
update object 0 (3) 0.37865222 0.8256836 6.2811527 0.0 0.0 -2.0941553 0.0
update object 0 (3) 0.37865222 0.8243041 6.2811527 0.04756862 0.0 1.6402973 0.0
update object 0 (3) 0.37865222 0.8243041 6.2811527 0.0 0.0 -1.3591034 0.0
update object 0 (3) 0.37865222 0.8262572 6.2811527 0.052787162 0.0 1.4266801 0.0
update object 0 (3) 0.38131282 0.8319766 6.2811527 0.23362696 0.0 6.697769 0.0
update object 0 (3) 0.38131282 0.8319766 6.2811527 0.0 0.0 -6.8713803 0.0
update object 0 (3) 0.38131282 0.8319766 6.2811527 0.0 0.0 0.0 0.0
remove object 0 (3)
add object 5 (4) 0.3838997 0.7569657 0.0
update object 5 (4) 0.3838997 0.74766445 1.4853827 0.26574987 0.0 7.5928535 0.0
update object 5 (4) 0.3820777 0.7410607 1.4853827 0.19572896 0.0 -2.000597 0.0
update object 5 (4) 0.3820777 0.7364408 1.4853827 0.10499765 0.0 -2.0620756 0.0
update object 5 (4) 0.3820777 0.73477453 1.4853827 0.057456825 0.0 -1.6393389 0.0
update object 5 (4) 0.38115984 0.73477453 1.4853827 0.035301536 0.0 -0.8521265 0.0
update object 5 (4) 0.38115984 0.7383939 1.4853827 0.097820885 0.0 1.6897122 0.0
update object 5 (4) 0.38267866 0.7416719 1.4853827 0.10322214 0.0 0.15432154 0.0
update object 5 (4) 0.38267866 0.7416719 1.4853827 0.0 0.0 -4.1288853 0.0
update object 5 (4) 0.38267866 0.7416719 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38355276 0.7416719 1.4853827 0.024974687 0.0 0.7135625 0.0
update object 5 (4) 0.38355276 0.74281234 1.4853827 0.04751732 0.0 0.9392763 0.0
update object 5 (4) 0.38355276 0.74281234 1.4853827 0.0 0.0 -1.3576376 0.0
update object 5 (4) 0.38355276 0.74281234 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38355276 0.7438743 1.4853827 0.03218109 0.0 0.9751846 0.0
update object 5 (4) 0.3850224 0.7457728 1.4853827 0.07275257 0.0 1.229439 0.0
update object 5 (4) 0.3850224 0.7457728 1.4853827 0.0 0.0 -2.2046237 0.0
update object 5 (4) 0.3850224 0.7457728 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.3850224 0.7446494 1.4853827 0.029562335 0.0 0.77795625 0.0
update object 5 (4) 0.3850224 0.7446494 1.4853827 0.0 0.0 -0.9854112 0.0
update object 5 (4) 0.3850224 0.7446494 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.3850224 0.74582744 1.4853827 0.03681332 0.0 1.1504161 0.0
update object 5 (4) 0.38592112 0.74582744 1.4853827 0.028084962 0.0 -0.27276114 0.0
update object 5 (4) 0.38592112 0.74582744 1.4853827 0.0 0.0 -0.85105944 0.0
update object 5 (4) 0.38592112 0.74703616 1.4853827 0.03555066 0.0 1.0456077 0.0
update object 5 (4) 0.38592112 0.74703616 1.4853827 0.0 0.0 -1.1109581 0.0
update object 5 (4) 0.38592112 0.7480674 1.4853827 0.029463427 0.0 0.8418122 0.0
update object 5 (4) 0.38727325 0.7480674 1.4853827 0.04097404 0.0 0.34880644 0.0
update object 5 (4) 0.38727325 0.7504541 1.4853827 0.0769919 0.0 1.1618665 0.0
update object 5 (4) 0.38846976 0.751919 1.4853827 0.054039527 0.0 -0.6557822 0.0
update object 5 (4) 0.38846976 0.751919 1.4853827 0.0 0.0 -1.1497772 0.0
update object 5 (4) 0.38846976 0.7534999 1.4853827 0.034368556 0.0 0.7471425 0.0
update object 5 (4) 0.38846976 0.7534999 1.4853827 0.0 0.0 -1.2274483 0.0
update object 5 (4) 0.38846976 0.7534999 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38846976 0.7545311 1.4853827 0.042965014 0.0 1.7902089 0.0
update object 5 (4) 0.38846976 0.75573987 1.4853827 0.048351288 0.0 0.21545097 0.0
update object 5 (4) 0.38846976 0.75573987 1.4853827 0.0 0.0 -1.4220966 0.0
update object 5 (4) 0.38846976 0.75573987 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38936844 0.75927395 1.4853827 0.11395484 0.0 3.5610886 0.0
update object 5 (4) 0.39017427 0.76237434 1.4853827 0.04928318 0.0 -0.99494874 0.0
update object 5 (4) 0.39233226 0.76737326 1.4853827 0.15556657 0.0 3.0366688 0.0
update object 5 (4) 0.3962385 0.7761623 1.4853827 0.2914553 0.0 4.11784 0.0
update object 5 (4) 0.3962385 0.7761623 1.4853827 0.0 0.0 -7.87717 0.0
update object 5 (4) 0.3962385 0.7761623 1.4853827 0.0 0.0 0.0 0.0
remove object 5 (4)

so I played around with this some more and finally got it to work. The problem was I was continuously drawing the movies in the wrong place as they needed to be within the vector in the draw section, and I needed an if statement to call them when a certain symbol was shown. code below :
/*
TUIO processing demo - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten#iua.upf.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// we need to import the TUIO library
// and declare a TuioProcessing client variable
import TUIO.*;
import java.util.*;
import processing.video.*;
TuioProcessing tuioClient;
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 0;
float table_size = 760;
float scale_factor = 1;
PFont font;
PImage img;
Movie theMov;
Movie theMov2;
PImage bg;
void setup()
{
//size(screen.width,screen.height);
size(1280,720);
noStroke();
fill(0);
//bg = loadImage("bg.png");
theMov = new Movie(this, "isobar.mov");
loop();
frameRate(30);
//noLoop();
font = createFont("Arial", 18);
scale_factor = height/table_size;
// we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods (see below)
tuioClient = new TuioProcessing(this);
}
// within the draw method we retrieve a Vector (List) of TuioObject and TuioCursor (polling)
// from the TuioProcessing client and then loop over both lists to draw the graphical feedback.
void draw()
{
//background(bg);
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
pushMatrix();
//translate(tobj.getScreenX(width),tobj.getScreenY(height));
rotate(tobj.getAngle());
rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
popMatrix();
if (tobj.getSymbolID()==0){
image(theMov, 0, 0);
}
if (tobj.getSymbolID()==5){
image(theMov2, 0, 0);
}
//fill(255);
//text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
}
}
// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
if (tobj.getSymbolID() == 0){
theMov.play();
}
if (tobj.getSymbolID() == 5){
theMov2.play();
}
if (tobj.getSymbolID()==1){
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
fill(255);
translate(tobj.getScreenX(width),tobj.getScreenY(height));
}
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
if ( tobj.getSymbolID() == 0) {
theMov.stop();
}
if ( tobj.getSymbolID() == 5) {
theMov2.stop();
}
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
void movieEvent(Movie m) {
m.read();
}

Related

How to convert sparse matrix to dense matrix in Julia

How do you convert a sparse matrix to a dense matrix in Julia? According to this I should be able to use full or Matrix, however full is evidently not standard in the SparseArrays module, and when I try to use Matrix:
I = []
J = []
A = []
for i in 1:3
push!(I, i)
push!(J, i^2)
push!(A, sqrt(i))
end
sarr = sparse(I, J, A, 10, 10)
arr = Matrix(sarr)
I get this error:
Exception has occurred: MethodError
MethodError: no method matching zero(::Type{Any})
It is enough to do collect(sarr) or Matrix(sarr).
Note, however that your code uses untyped containers which is not recommended. Indexes in arrays are Ints so it should be:
I = Int[]
J = Int[]
A = Float64[]
for i in 1:3
push!(I, i)
push!(J, i^2)
push!(A, sqrt(i))
end
sarr = sparse(I, J, A, 10, 10)
Now you can do:
julia> collect(sarr)
10×10 Matrix{Float64}:
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 1.41421 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.73205 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

Julia: How to execute functions in parallel?

I want to run functions in parallel. These functions are executed many times in a loop.
coordSys = SharedArray{Bool}([true,false,true,true]);
dir = SharedArray{Int8}([1,2,3,2]);
load = SharedArray{Float64}([8,-7.5,7,-8.5]);
L = SharedArray{Float64}([400,450,600,500]);
r = SharedArray{Float64}([0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0]);
Obviously these vectors will be huge, but for simplicity I just put this limited size.
Operation without parallel computing:
function unifLoad(coordSys,dir,load,L,ri)
if coordSys == true
if dir == 1
Q = [load;0;0];
elseif dir == 2
Q = [0;load;0];
elseif dir == 3
Q = [0;0;load];
end
q = ri*Q; #matrix multiplication
P = q[1]*L/2;
V = q[2]*L/2;
M = -q[3]*L*L/12;
f = [P;V;M];
else
f = [1.0;1.0;1.0];
end
return f
end
running the loop:
var = zeros(12)
for i = 1:length(L)
var[3*(i-1)+1:3*i] = unifLoad(coordSys[i],dir[i],load[i],L[i],r[3*(i-1)+1:3*i,:]);
end
The returned value is:
var
12-element Array{Float64,1}:
0.0
0.0
-1.06667e5
1.0
1.0
1.0
2100.0
0.0
-0.0
0.0
2125.0
-0.0
Operation with parallel computing
I've been trying to implement the same function in parallel, but without getting the same results.
# addprocs(3)
#everywhere function unifLoad_Parallel(coordSys,dir,load,L,ri)
if coordSys == true
if dir == 1
Q = [load;0;0];
elseif dir == 2
Q = [0;load;0];
elseif dir == 3
Q = [0;0;load];
end
q = ri*Q; # Matrix multiplication (ri -> Array 3x3)
P = q[1]*L/2;
V = q[2]*L/2;
M = -q[3]*L*L/12;
f = [P;V;M];
else
f = [1.0;1.0;1.0];
end
return f
end
running the parallel loop:
var_parallel = SharedArray{Float64}(12);
#parallel for i = 1:length(L)
var_parallel[3*(i-1)+1:3*i] = unifLoad_Parallel(coordSys[i],dir[i],load[i],L[i],r[3*(i-1)+1:3*i,:]);
end
The returned value is:
var_parallel
12-element SharedArray{Float64,1}:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
On my Julia 0.6.3 the parallel code returns the same result so I am unable to reproduce the problem (also I do not encounter the issue #SalchiPapa reports).
However, I would like to note that this code actually should work faster with threads (I assume that the real problem is much larger). Here is the code you could use (I used an equivalent implementation to your which is a bit shorter - but the only significantly relevant change is that I wrap it in a function which provides dramatic performance gains). The crucial issue that all arrays except var are shared but only read. And var is written but only once at each entry and not read from. This is the case where it is safe to use threading which has a lower overhead.
Here is an example code (you have to define JULIA_NUM_TREADS environment variable before starting Julia and set it to number of threads you want - most probably 4 is what you want):
using Base.Threads
function experiment()
coordSys = [true,false,true,true];
dir = [1,2,3,2];
load = [8,-7.5,7,-8.5];
L = [400,450,600,500];
r = [0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0];
unifLoad(coordSys,dir,load,L,r, i) =
coordSys ? load * L * r[3*(i-1)+1:3*i, dir] .* [0.5, 0.5, -L/12] : [1.0, 1.0, 1.0]
var = zeros(12)
#threads for i = 1:length(L)
var[3*(i-1)+1:3*i] = unifLoad(coordSys[i],dir[i],load[i],L[i],r,i);
end
var
end
Also here is a bit simplified code for parallel processing using similar ideas:
coordSys = SharedArray{Bool}([true,false,true,true]);
dir = SharedArray{Int8}([1,2,3,2]);
load = SharedArray{Float64}([8,-7.5,7,-8.5]);
L = SharedArray{Float64}([400,450,600,500]);
r = SharedArray{Float64}([0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0]);
#everywhere unifLoad(coordSys,dir,load,L,r,i) =
coordSys ? load * L * r[3*(i-1)+1:3*i, dir] .* [0.5, 0.5, -L/12] : [1.0, 1.0, 1.0]
vcat(pmap(i -> unifLoad(coordSys[i],dir[i],load[i],L[i],r,i), 1:length(L))...)
Here pmap is mostly used to simplify the code so that you do not need #sync.

Diagonalization of symmetric matrices in julia yields strange results

I would like to understand what is going on with my program in julia. The issue is the following: I have a symmetric non-negative matrix which I diagonalize using
egvals, egvecs = eig(H_mat)
By a theorem, my matrix should have a maximum eigenvalue which is associated a non-negative eigenvector. The H_mat has a further trick, its first column and row has an entry filled with zeros.
Diagonalization yields a maximum positive eigenvalue E_max, in fact it is the last eigenvalue because julia arranges the eigenvalues in order up to the biggest, but my eigenvector associated with E_max does not have all its entries zero or positive (i.e. they have negative entries)
egvecs[:,end] # Some or several components ii, egvecs[ii,end]<0
This is the matrix from I don't get proper results, for instance:
[0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 2.0 0.0 0.0 1.414213562373095 0.0 0.0 0.0 0.0 0.0 1.414213562373095 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 3.0 0.0 0.0 1.7320508075688774 0.0 0.0 0.0 0.0 0.0 1.7320508075688774 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 1.414213562373095 0.0 0.0 2.0 0.0 1.414213562373095 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 1.7320508075688774 0.0 0.0 3.0 0.0 2.0 0.0 0.0 0.0 1.0 0.0 1.414213562373095 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 1.414213562373095 0.0 2.0 0.0 0.0 0.0 0.0 0.0 1.414213562373095 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 2.0 0.0 3.0 1.7320508075688774 0.0 0.0 0.0 0.0 1.414213562373095 1.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.7320508075688774 3.0 0.0 0.0 0.0 0.0 0.0 1.7320508075688774 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 1.414213562373095 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 2.0 0.0 1.0 0.0 0.0 1.414213562373095 0.0 0.0 0.0
0.0 0.0 0.0 1.7320508075688774 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 1.414213562373095 0.0 0.0 2.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.414213562373095 0.0 0.0 0.0 1.0 0.0 2.0 0.0 0.0 1.414213562373095 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 1.414213562373095 0.0 1.414213562373095 0.0 0.0 0.0 1.414213562373095 0.0 3.0 1.414213562373095 0.0 1.414213562373095 1.414213562373095 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.7320508075688774 0.0 0.0 0.0 0.0 1.414213562373095 3.0 0.0 0.0 2.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.414213562373095 0.0 1.414213562373095 0.0 0.0 2.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 2.0 0.0 1.414213562373095 0.0 0.0 3.0 1.0 1.7320508075688774
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.414213562373095 2.0 0.0 1.0 3.0 1.7320508075688774
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.7320508075688774 1.7320508075688774 3.0]
(Should be a 20X20 matrix)
Eigenvectors are only determined up to proportionality because the requirement is that they solve (A - lambda*I)v = 0 and if v1 solves the equation, then also v2 = -v1 does. In the symmetric case, it is standard to normalize all the vectors to one, but that still leaves the sign undetermined. Therefore the theorem you are using must say something like that the vector corresponding to the largest values can be chosen such that it has non-negative elements. Indeed, I get that the eigenvector corresponding to the largest eigenvalue of your matrix has non-positive elements.
julia> eig(A) |> t -> all(t[2][:,indmax(t[1])] .<= 0)
true

How to edit a specific pattern according to an array of patterns?

I have a file containing the string "proto" which is repeated 384 times. Each "proto" should be edited according to 384 different labels existing in another textfile. For insance, if the content of the second text file is a, sp, .. (each label exists in a newline), so in my textfile the first "proto" should be changed to "a", the second to "sp" and so on. How should I do that?
The original file is a 384 repetitions of :
~o <VecSize> 39 <MFCC_0_D_A>
~h "proto"
<BeginHMM>
<NumStates> 5
<State> 2
<Mean> 39
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
<Variance> 39
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
<State> 3
<Mean> 39
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
<Variance> 39
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
<State> 4
<Mean> 39
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
<Variance> 39
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
<TransP> 5
0.0 1.0 0.0 0.0 0.0
0.0 0.6 0.4 0.0 0.0
0.0 0.0 0.6 0.4 0.0
0.0 0.0 0.0 0.7 0.3
0.0 0.0 0.0 0.0 0.0
<EndHMM>
and the second file contains 384 labels, such as: a, ap, k12, sp, ... So as you see, I have just one form of "proto". I hope it is now clear.
Assuming textfile with replacement patterns has exact 384 lines you can use this awk command:
awk 'FNR==NR{a[++i]=$0; next} /proto/{sub(/proto/, a[++j])} 1' replacement.txt file.txt
This might work for you (GNU sed):
sed 's|.*|s/proto/&/|' replacementFile | sed -e ':a;$!{N;ba}' -f - textFile
This generates a substitution command for every replacement in the replacementFile and reads this command set into sed script which has slurped the textFile into memory.
N.B. This assumes that there is a replacement for every pattern in the textFile.

Create Image from Text File RGB Data in Matlab

I have a text file with RGB data in the form of:
[Pixel 0,0] [Pixel 1,0] [Pixel 2,0]...
[Pixel 0,1] [Pixel 1,1] [Pixel 2,2]...
...
With an input of:
0.0 0.0 0.0 <-- this would be Pixel 0,0
1.0 0.0 0.0
1.0 0.9 0.0
I can create the flag of Germany in size 3x1 with:
%load the data to myData
Germany = reshape(myData,3,1,3);
image(Germany)
The 1px-wide pattern works good as show in picture, however, the goal is to be able to create multiple patterns, e.g. the Germany flag in 3x3 followed by Romania flag in 3x3 or any other pattern of any length and doing that! is where I can not find the proper way to reshape the matrix.
The input that should create the second example shown in picture is this:
|========= Germany Flag ==========| [ Blue ] [ Yellow ] [ Red ]
Black -> 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
Red -> 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
Yellow-> 1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.9 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
Any help is appreciated
Update: Asked by Marcin, the input files are literal as I explained above.
This is the content of the GermanyRomania.txt file:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.9 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
With that file I must create the 2nd pattern in picture (German+Romania Flag), there is ALL the RGB info required to do it.
I don't think you can achieve what you want by simply using the reshape function.
We must take into account that Matlab stores matrices in column-major order (you can read more about it here).
Therefore, before we can use the reshape function, we must have the data matrix in the following format:
[Pixel 0,0]
[Pixel 0,1]
...
[Pixel 1,0]
[Pixel 1,1]
...
[Pixel n,n]
Here's a possible solution:
# data stores the input
height = size(data, 1)
width = size(data, 2)
vertical_data_cell = mat2cell(data, height, 3 * ones(1, width / 3))'
vertical_data = cell2mat(vertical_data_cell)
flags = reshape(vertical_data, height, width / 3, 3)
image(flags)
Note that we make the matrix transformation on lines 4 and 5.
And here is the result for the input you provided:
It also works with different heights.
Here's the input for the flags of Germany, Argentina and Portugal.
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.46 0.66 0.85 0.46 0.66 0.85 0.46 0.66 0.85
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 1.0 0.98 0.75 0.29 1.0 1.0 1.0
1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.9 0.0 0.46 0.66 0.85 0.46 0.66 0.85 0.46 0.66 0.85
0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
And this is the result:

Resources