Tuesday, August 27, 2013

Scicoslab/Scipad: Upgrading from 8.70 to 8.71

Note: This guide is for Linux.

  1. Download from source
  2. Instructions are here: installation guide
  3. However, to move files from the unzipped source to the scicoslab directory ( /usr/lib/scicoslab-gtk-4.4.1/), use
  4. sudo cp -r scipad/*  /usr/lib/scicoslab-gtk-4.4.1/
    

    The lesson here is that mv command doesn't have -r option, so cp is all right.

  5. Then start scicoslab with root privilege
  6. sudo scicoslab
    
  7. Issue the following under scicoslab
  8. genlib("utillib");
    

    Exit and restart scicoslab, and that will be okay.

Tuesday, August 20, 2013

A Hands-on Guide to ScicosLab - Part 2

Define a Zero Vector

--> v = zeros(1:20)

Use Scipad Editor

To launch the embedded editor, type

--> scipad

One can write a sequence of Scicos instructions and save it as *.sci files. To load such a program into ScicosLab, type

--> exec('foo.sci');

Directory operation

It inherits a Linux style,

--> pwd
--> ls
--> cd

To clear terminal screen, use either one

--> ctrl+l
--> clc

Define a Function

For instance,

function [re,im] = compute(v, f)
    re=re+cos(2*%pi*f*v/1000000);
    im=im+sin(2*%pi*f*v/1000000);
    return [re,im];
  endfunction

The return value is a vector [re,im], as you might notice, the Scicos language is C-like but not strong-typed. This function compute takes two params. Notice that the incremental sum is not supported, that is re+=... commits an error. Once loaded into ScicosLab, it works as library functions. You can call it anytime, but after a clear command, it vanishes as all the other customized variables do.

For and While

for i=1:20, printf("%d\n",i); end

Notice that no increment of i is needed. It does so automatically at every loop.

i=1;
while i<=20, printf("%d\n",i);i++; end

TBD.

Wednesday, August 14, 2013

A Short Recap on Fourier's Toys

Fourier's building blocks were sine and cosine waves. All the majesty of periodic functions can be constructed by them.

Fourier series

Just a glimpse. A periodic function $f(x)$, with $(-\pi, \pi)$ as its basic periodic unit, can be approached by the sum of an infinite series.

$$a_0 = \frac{1}{2\pi} \int_{-\pi}^{\pi} f(x)dx \\ a_n = \frac{1}{\pi} \int_{-\pi}^{\pi} f(x)\cos(nx)dx \\ b_n = \frac{1}{\pi} \int_{-\pi}^{\pi} f(x)\sin(nx)dx \\ a_0 + \sum_{n=1}^{\infty}(a_n \cos (nx) + b_n \sin (nx))$$

Fourier Transform

Also a quick glance,

$$\mathcal{F}(f(t))(s) = \int_{-\infty}^{+\infty} f(t)e^{-2\pi i s t} dt$$

Discrete Fourier Transform (DFT)

A bit into the point. The input of DFT is a finite series of equally-spaced (in time) samples. e.g., f(0), f(0.1), f(0.2), ..., f(2π), and the output is a series of complex numbers encoding the amplitude and phase information, say $\mathcal{F}(0)$, $\mathcal{F}(F_0)$, ..., $\mathcal{F}(-F_0)$. And $F_0$ is the fundamental frequency, which is calculated as

$$F_0 = \frac{1}{N \Delta}$$

$\Delta$ is the sample period, in our example it is $0.1$. The x-axis of these numbers are the frequency spaced as in the figure below,

However, the discrete transform is thus performed, $$\mathcal{F}(n) = \sum_{k=0}^{N} f(k) e^{-2\pi i n k/N}$$

Sunday, August 11, 2013

A Hands-on Guide to ScicosLab - Part 1

The basic calculation unit is a vector or a matrix in ScicosLab. What we get used to is a 1*1 matrix.

Define a Vector

--> 0:0.1:4*%pi

Well, the result is stored in the variable ans and the vector will be displayed hereafter. This vector is comprised of float numbers from 0 to 4π with a spacing of 0.1, they are 116 numbers, check this with the function length(). Yes, %pi is the Scicos way of pre-defining some constants. For the logarithmic natural base e, it uses %e. To stop printing results after each command, one can end it with a semi-colon(;).

Use Variables

--> a = 0:0.1:4*%pi;

Variables are type-free when being declared, so the initialization will make the variable a being a vector. Any assignment will change its content and type just like using a brand-new variable. The system preserved variable ans will be updated in the same fashion after executing every command.

--> a = sin(a);

Plot a Sine Wave

--> plot(a);

This gives you a sine wave of 2 periods.

Perform Discret Fourier Transform

--> v = dft(a,-1);
--> clf();
--> plot(v);

To check the usage of a function or related entries, use

--> apropos fourier
--> help dft

To clear the graph/figure, use the function clf(). We see in the new plotting

ScicosLab just drew the real part of frequency components, to compare with the imaginary parts, use

--> plot(imag(v), 'red');

The figure gets re-shaped and we see two spikes again. As a matter of fact, the Fourier transform of sine wave is two dirac functions.

Variables Revisited

--> browsevar
--> clear

The former gives a window of currently being-used variables, and the latter command just destroys them.

Friday, August 9, 2013

Read Data Text Files into A Vector in Scicoslab

The data file temp is lines of float numbers with one at each line.

--> fp = mopen("temp","r");
--> v = mfscanf(-1,fp,"%f");
--> v';

A Note on Ubuntu Dash Icons

In principle, dash application icons are located under /usr/share/icons/hicolor/apps, however, this can be customized. For instance, the configuration for a program named ScicosLab lies in /usr/share/applications/scicoslab.desktop, it has the following content:

[Desktop Entry]
Name=ScicosLab
Comment=Scientific Computing using ScicosLab
TryExec=/usr/bin/scicoslab
Exec=/usr/bin/scicoslab
Icon=/usr/lib/scicoslab-gtk-4.4.1/config/puffin-gtk48.png
Terminal=false
Type=Application
Categories=Education;Science;Math;
StartupNotify=false
X-Desktop-File-Install-Version=0.15

One can restart the system to see the effect.

To regenerate the cache

sudo gtk-update-icon-cache --force /usr/share/icons/hicolor

Saturday, June 1, 2013

Symoblic CTL Model Checking

Ordered Binary Decision Diagram

Boolean formulas are represented in diagrams. OBDD has the following property, $$\phi \leftrightarrow \psi \Rightarrow OBDD(\phi) = OBDD(\psi)$$

Symbolic Denotation $\xi$

$\xi : S \rightarrow $boolean formulas (OBDDs). By definition, $$\xi([\phi]) := \xi(\{ s | M,s \models \phi \})$$

A list of some symbolic encodings,

  • $\xi([\mathbf{EX}\phi]) = \exists V' . (\xi ([\phi])[V'] \wedge \xi (R)[V,V'])$
  • $\xi([\mathbf{EG}\phi]) = \nu Z . (\xi([\phi]) \wedge \xi([\mathbf{EX}Z]))$
  • $\xi([\mathbf{E}(\phi\mathbf{U}\psi)]) = \mu Z . (\xi([\psi]) \vee (\xi([\phi]) \wedge \xi([\mathbf{EX}Z])))$

The above exactly parallels the classical fixed-points techniques. See a previous post (link).

Fixed-point Algorithms

The following shows the symbolic counterpart for the classical fixed points algorithms (denotational, based on states). Recall their inductive version,

  • $[\mathbf{EG}\phi]$: $X_{j+1} = X_j \cap \mathsf{PreImage}(X_j)$
  • $[\mathbf{E}(\phi\mathbf{U}\psi)]$: $X_{j+1} = X_j \cup ([\phi] \cap \mathsf{PreImage}(X_j))$
PreImage
OBDD PreImage(OBDD X){
  return ∃V'. (X[V'] ∧ ξ(R)[V,V']);
}
Check_EG
OBDD Check_EG(OBDD X) {
    Y′ := X;
    repeat
        Y := Y′;
        Y′ := Y ∧ PreImage(Y);
    until (Y′ ↔ Y);
    return Y;
}
Check_EU
OBDD Check_EU(OBDD X1 ,X2) {
    Y′ := X2;
    repeat
        Y := Y′;
        Y′ := Y ∨ (X1 ∧ PreImage(Y));
    until (Y′ ↔ Y);
    return Y;
}
Check_FairEG

Here we need to refer Emerson Lei algorithm which specifies the fair CTL version. $$ [\mathbf{E}_f \mathbf{G} \phi ] = \nu Z .([\phi] \cap \bigcap_{F_i \in FT} [\mathbf{EX}[\mathbf{E}(Z \mathbf{U}(Z \wedge F_i ))])$$

The can be interpreted as $Z$ keeps being true until all fair conditions are met when $Z$ is still true at each step of iteration.

OBDD Check_FairEG(OBDD X) {
    Z’ := X;
    repeat
        Z := Z’;
        for each Fi in FT
            Y := Check_EU(Z, Fi ∧ Z);
            Z’:= Z’ ∧ PreImage(Y);
        end for;
    until (Z’ ↔ Z);
    return Z;
}

Check Invariants

Invariants are specified as $\mathbf{AG}\phi$. Its negation is $\neg \mathbf{EF} \neg \phi$, which is a reachability issue: are there such states which can be reached from the initial states? We show the forward checking technique.

OBDD Compute_reachable() {
    Y′ := I;
    Y := ⊥;
    while ¬(Y′ ↔ Y) {
        Y := Y′;
        Y′ := Y ∨ Image(Y);
    }
    return Y;
}

Note backward checking iteratively calculates the preimage of $\phi$ until a fixed-point is reached or the preimage intersects with $I$. An advanced version with the so-called frontier (F) is presented below.

OBDD Compute_reachable() {
    Y := I;
    F := I;
    while ¬(F ↔ ⊥) {
        F := Image(F) ∧ ¬Y;
        Y := F ∨ Y;
    }
    return Y;
}

If we explain it in the set flavor, it could be at each time we only calculate the image of new states (frontier). The set of reachable states is saturated when the image of the frontier can't bring in more new states.

Forward_Check_EF

Based on the discussion above, we now can check invariants (often feature as bad behaviors).

bool Forward_Check_EF(OBDD BAD) {
    Y := F := I;
    while ¬(F ↔ ⊥) and (F ∧ BAD) ↔ ⊥ {
        F := Image(F ) ∧ ¬Y ;
        Y := Y ∨ F ;
    }
    if F = ⊥
        return false
    else
        return true
}

The above utilizes the frontier technique. It looks for all reachable states and verifies whether there are some bad states within them.

References & Readings

  1. R. Sebastiani, Symbolic CTL Model Checking, slides on Formal Methods, 2012
  2. McMillan, Symbolic Model Checking, a Carnegie-Mellon PhD dissertation, 1993
  3. Wikipedia, Fair Computational Tree Logic, http://en.wikipedia.org/wiki/Fair_computational_tree_logic

Appendix

The most of this post is heavily extracted from the RS's teaching slides, hence I don't claim the copyright. The character of this post is intended to be a note, with marginal and personal remarks. For those who need to respect the source, please read the following copy of the original copyright notice.

Copyright notice: some material (text, figures) displayed in these slides is courtesy of M. Benerecetti, A. Cimatti, P. Pandya, M. Pistore, M. Roveri, and S.Tonetta, who detain its copyright. Some exampes displayed in these slides are taken from [Clarke, Grunberg & Peled, “Model Checking”, MIT Press], and their copyright is detained by the authors. All the other material is copyrighted by Roberto Sebastiani. Every commercial use of this material is strictly forbidden by the copyright laws without the authorization of the authors. No copy of these slides can be displayed in public without containing this copyright notice.