Friday, February 10, 2012

Using tikz-uml to create uml diagrams in latex

At Uni we are learning UML for documenting our products. Therefore we need at tool to create these. I had a hard time finding a good, free, gui tool, so I settled for a LaTeX/TikZ solutions, since we use LaTeX for reports anyway. I will here briefly describe how to get started using tikz-uml, as the package is called.

First step is to go and download tikz-uml here.

Assuming you are on linux, unzip the tikz-uml.sty file in a folder: ~/texmf/tex/latex/tikz-uml/. Windows people should be able to find a similar folder in their C:\Program Files\Miktex folder somewhere, as far as I recall.

Now run the command:

texhash ~/texmf
to make Latex aware of the newly installed package. (I do not know how to do this in Windows. Possibly through the Miktex package manager?)

Now you are ready to get stated using tikz-uml. Usage examples are found here. A simple template to get started could be:

\documentclass[12pt,article]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\linespread{1.05}
\usepackage{tikz}
\usepackage{tikz-uml} 
\usepackage{amsmath}
\begin{document}

\begin{tikzpicture}
\begin{umlsystem}[x=0, y=0]{Name of system} 
% Code from official documentation goes here...
\end{umlsystem}
% And here
\end{tikzpicture}

\end{document}

Example of Use Case model

Here is the code for a use case diagram using tikz-uml:

\documentclass[12pt,article]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\linespread{1.05}
\usepackage{tikz}
\usepackage{tikz-uml} 
\usepackage{amsmath}
\usepackage{savetrees}
\begin{document}

\begin{figure}[h!]
\begin{tikzpicture}
\begin{umlsystem}{Schedule}
\umlusecase[x=-3]{Check Request}
\umlusecase[x=3]{Ask Constraints}
\umlusecase[x=-2,y=-2]{Deny Request}
\umlusecase[x=3, y=-2]{Collect Constraints}
\umlusecase[x=-3,y=-4]{Determine Schedule}
\umlusecase[x=3,y=-4]{Merge Constraints} 
\umlusecase[y=-6]{Resolve Conflicts}
\end{umlsystem}

\umlactor[x=-8]{Initiator}
\umlactor[x=-8,y=-6]{Participant A}
\umlactor[x=8]{Participant B}
\umlactor[x=8,y=-6]{Conflict Resolver} 

\umlassoc{Initiator}{usecase-1}
\umlassoc{Initiator}{usecase-3}
\umlassoc{Participant A}{usecase-5}
\umlassoc{Participant B}{usecase-2}
\umlassoc{Participant B}{usecase-4}
\umlassoc{Conflict Resolver}{usecase-7}

\umlextend{usecase-2}{usecase-3}
\umlinclude{usecase-5}{usecase-7}
\end{tikzpicture}
\caption{Example of Use case diagram using tikz-uml. Examlpe from the book Requirements Engineering by Axel van Lamsweerde.}
\end{figure}

\end{document}
and the result:

More examples may come as we learn more about it.

Simplifying trigonometric products into trigonometric sums

It can be hard to differentiate products of trigonometric functions like this one: \[ \cos{y}\sin{y} \] It is easier when the functions are separated like this: \[ \cos{y}+\sin{y} \] for example. Therefore I will here describe how to transform equations of the first form into equations of the second form using the Euler's formula.

Euler's formula lets us transform the $\sin$ and $\cos$ parts into this: \[ \begin{aligned} \DeclareMathOperator{\e}{e} \cos(y) &= \frac{\e^{iy} + \e^{-iy}}{2} \\ \sin(y) &= \frac{\e^{iy} - \e^{-iy}}{2i} \end{aligned} \]

By doing this we can make use of the rules for products of $\e$, which are preserved for complex numbers: \[ \e^{z_1}\;\e^{z_2} = \e^{z_1+z_2} \] and magically doing this will end up with some parts that can be put back on the regular trigonometric form be reversing Euler's formula.

Worked example

I will now rewrite trigonometric products like this: \[ \sin{3x}\cos{5x} = \frac{1}{2} (\sin{8x} - \sin{2x}). \]

First, I will rewrite the left side using Euler's formula: \[ \left( \frac{\e^{3xi} - \e^{-3xi}}{2i} \right) \left( \frac{\e^{5xi} + \e^{-5xi}}{2} \right). \]

I pull the denominator out in front, and use the product rules of $\e$: \[ \begin{aligned} &\frac{1}{4i} (\e^{3xi + 5xi} + \e^{-3xi + 5xi} - \e^{-3xi + 5xi} - \e{-3xi - 5xi}) \\ = &\frac{1}{4i}(\e^{8xi} + \e^{-2xi} - \e{2xi} - \e^{-8xi}) \end{aligned} \]

I break it up in two parts: \[ \frac{1}{4i} (\e^{8xi} - \e^{-8xi}) + \frac{1}{4i} (\e^{-2xi} - \e^{2xi}) \]

Now what we are trying to do is to get it on the form defined by Euler's formula. Because there is a minus between the $\e$-s in both the first and the second part, we assume that we want to go back to sinus in both cases. Sinus was defined as: \[ \sin(y) = \frac{\e^{iy} - \e^{-iy}}{2i} \] and therefore part of the denominator of $\frac{1}{4i}$, is “pulled” into the equation like this: \[ \frac{1}{2} \left(\frac{\e^{8xi} - \e^{-8xi}}{2i}\right) + \frac{1}{2} \left(\frac{\e^{-2xi} - \e^{2xi}}{2i}\right) \]

Now the “equation” is on the Euler's form, and can be put back as an ordinary trigonometric function. Taking the signing into condition, it ends up like this: \[ \frac{1}{2} (\sin{8x} - \sin{2x}) \] and thereby the first statement is proved.

Solving roots in binomial complex equations

In this post I will describe how to solve binomial complex equations of the form \[ z^n = a + bi. \]

This is done by first putting the right side on polar form using the following: \[ \begin{aligned} R &= \sqrt{a^2 + b^2} \\ \theta &= \left\{ \begin{aligned} &\tan^{-1}\frac{b}{a} &\text{if}\; a > 0 \\ &\tan^{-1}\frac{b}{a} + \pi &\text{if}\; a < 0 \\ &\frac{\pi}{2} &\text{if}\; a = 0, b > 0 \\ &-\frac{\pi}{2} &\text{if}\; a = 0, b < 0 \\ &\text{random} &\text{if}\; a = 0, b = 0 \end{aligned} \right. . \end{aligned} \]

The roots are now found using the following: \[ z = \left( \sqrt[n]{R} \right)_{\frac{a}{n} + p\frac{2\pi}{n}} \qquad p = 0, 1, \ldots, n-1 \] where p is altered from $0$ to $n-1$, to find all roots.

Worked example

Given: \[ z^4 = 3+4i \]

Finding $R$: \[ \begin{aligned} R &= \sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5 \\ R &= 5 \end{aligned} \]

Finding $\theta$ (note: $a = 3 \Leftrightarrow a > 0$): \[ \theta = \tan^{-1}\frac{4}{3} \]

The roots are therefore: \[ \begin{aligned} z_0 &= \left( \sqrt[4]{5} \right)_{\frac{3}{4}} \approx 1.495_{0.75}\\ z_1 &= \left( \sqrt[4]{5} \right)_{\frac{3}{4} + 1\frac{2\pi}{4}} \approx 1.495_{2.32}\\ z_2 &= \left( \sqrt[4]{5} \right)_{\frac{3}{4} + 2\frac{2\pi}{4}} \approx 1.495_{3.89}\\ z_4 &= \left( \sqrt[4]{5} \right)_{\frac{3}{4} + 3\frac{2\pi}{4}} \approx 1.495_{5.46} \end{aligned} \] in polar coordinates. To reverse to regular coordinates, just use the following: \[ \begin{aligned} a &= R\cos{\theta} \\ b &= R\sin{\theta} \end{aligned} \]

Looking at the complex plane the roots are distributed as follows:

Monday, February 6, 2012

Complex numbers: Roots in polynomials of degree two

Here is how to solve the roots in polynomials of degree two with complex numbers. After explaining the approach I will go through an example.

We have a polynomial of the form: \[ p(z) = az^2 + bz + c \] To find the roots of this, we need to know the discriminant $D$, to know if the roots are complex. This is the case if $D < 0$. \[ D = b^2 -4ac \] If $D \geq 0$ the root(s) are real, and may be found using the formula: \[ x = \frac{-b\pm \sqrt{D}}{2a} \] If the discriminant is negative or complex, we need replace $\sqrt{D}$ with $w$, which is the solution to the equation $z^2-D = 0$. This is found by using the formula: \[ w = \pm\left( \sqrt{\frac{r+\alpha}{2}} + i\,\text{sgn}(\beta)\sqrt{\frac{r-\alpha}{2}} \right) \] Where the discriminant is given as $D = \alpha + \beta i$ and $r = \sqrt{\alpha^2 + \beta^2}$. $\text{sgn}$ is $+1$ or $-1$ depending on the value of $\beta$: \[ \text{sgn}(\beta) = \left\{ \begin{aligned}1,\qquad \text{if}\; \beta \geq 0, \\ -1,\qquad \text{if}\; \beta < 0. \end{aligned} \right. \] Now the roots may be found using the formula: \[ z = \frac{-b \pm w}{2a} \] Using $a$, $b$, and $c$ from the original polynomial and $w$ just found.

Worked example

Here's a worked example with the polynomial \[ z^2 + 2z - (2+4i) = 0 \] First we find the discriminant: \[ D = b^2 - 4ac = 2^2 - 4\cdot 1\cdot (-(2+4i)) = 12 +16i \] As this is a complex determinant, we now need to find $w$. With the discriminant $D = 12 + 16i$, $r = \sqrt{12^2 + 16^2} = \sqrt{400} = 20$ and $\text{sqn}(\beta)=1$. \[ w = \pm\left( \sqrt{\frac{20+12}{2}} + i\cdot 1\cdot \sqrt{\frac{20-12}{2}} \right) = \pm \left( \sqrt{16} +i\sqrt{4} \right) = \pm (4-2i) \] This enables us to find the roots for the original polynomial: \[ \begin{align} z &= \frac{-b\pm w}{2a} = \frac{-2 \pm (4-2i)}{2\cdot 1} = \frac{-2}{2} \pm \frac{4-2i}{2} \\ z_1 &= -1 + 2-i = 1-i &\qquad\text{Using} + \\ z_2 &= -1 -2+i = -3+i &\qquad\text{Using} - \\ \end{align} \]

Using ZNC to keep you signed in on an IRC channel

In my study group at the University we are using IRC to communicate with each other. We have a channel set up on irc.freenode.net, so that it is only to be used by us.

The problem with IRC is, that if you are not logged in at the time another group member posts a message, you do not receive the message.

There are basically two ways to fix this, both including having access to an external server:

  1. Have a console based IRC client running on an external server and SSH into this server.
  2. Have a 'bot' always be logged in to the channel, and record the action happening there.

Since many of the members of my study group are not comfortable with the terminal, we settled for the second solution, using a service called znc. Here Is how to set it up for several users:

Setting up ZNC for multiple users:

First you have to SSH into the server you are using to record the IRC actions and install znc on it. Assuming it's a Linux server, it should be in the distro's repositories.

Now you have to set up znc - first for a single user, since for several users editing the configuration file created. Follow the instructions on the screen. Here's how I set it up (these steps are quite obvious/individual so you can probably skip reading the bullets and follow the guidance on-screen):

  • What port would you like ZNC to listen on? 65000
  • Would you like ZNC to listen using SSL? (yes/no) [no]: no
  • Would you like ZNC to listen using ipv6? (yes/no) [yes]: yes
  • Listen Host (Blank for all ips): (blank)
  • Load global module <partyline>? (yes/no) [no]: yes
  • Load global module <webadmin>? (yes/no) [no]: no
  • Username (AlphaNumeric): myusername
    • This is the username you use when you log on to znc through your client
  • Enter Password: mypassword
  • Confirm Password: mypassword
    • Likewise, password used when you log into znc
  • Would you like this user to be an admin? (yes/no) [yes]: yes
  • Nick [myusername]: mynick
  • Alt Nick [mynick_]: mynick_
  • Ident [mynick]:mynick
  • Real Name [Got ZNC?]: My Real Name
    • Nickname, alternative nicknames and real name on IRC
  • Bind Host (optional): (blank)
  • Number of lines to buffer per channel [50]: 5000
    • Number of lines to record in channel when you're away
  • Would you like to keep buffers after replay? (yes/no) [no]: yes
  • Default channel modes [+stn]: (blank)
  • Load module <admin>? (yes/no) [no]: no
  • Load module <chansaver>? (yes/no) [no]:no
  • Load module <keepnick>? (yes/no) [no]: no
  • Load module <kickrejoin>? (yes/no) [no]: no
  • Load module <nickserv>? (yes/no) [no]: no
  • Load module <perform>? (yes/no) [no]: no
  • Load module <simple_away>? (yes/no) [no]: no
    • These modules can be added if you want to :)
  • IRC server (host only): irc.freenode.net
    • Where your IRC is hosted
  • [irc.freenode.net] Port (1 to 65535) [6667]: 6667
  • [irc.freenode.net] Password (probably empty): (blank)
  • Does this server use SSL? (yes/no) [no]: no
  • Would you like to add another server for this IRC network? (yes/no) [no]: no
  • Would you like to add a channel for ZNC to automatically join? (yes/no) [yes]: yes
  • Channel name: #archlinux
    • This is where you put your channel to connect to - i our case it's our internal IRC, but for demonstration purposes I will use the #archlinux channel.
  • Would you like to add another channel? (yes/no) [no]: no
  • Would you like to set up another user (e.g. for connecting to another network)? (yes/no) [no]: no
    • I find it easier to just copy and paste from the config file
  • Launch ZNC now? (yes/no) [yes]: no

Now we need to edit the config file, to add all users. Open the file ~/.znc/configs/znc.conf using a text editor. It should look something like this:

// WARNING
//
// Do NOT edit this file while ZNC is running!
// Use webadmin or *admin instead.
//
// Buf if you feel risky, you might want to read help on /znc saveconfig and /znc rehash.
// Also check http://en.znc.in/wiki/Configuration

Listener   = 65000
LoadModule = partyline

<User myusername>
  Pass       = sha256#8947e2b4df7bde63624258d83dea6a35004ee8b7693ec64dd941e2401bbda08e#4sCV(V3I9kP,/Qw5t(Z8#
  Admin      = true
  Nick       = mynick
  AltNick    = mynick_
  Ident      = mynick
  RealName   = My Real Name
  Buffer     = 5000
  KeepBuffer = true
  ChanModes  = +stn


  Server     = irc.freenode.net 6667 

  <Chan #archlinux>
  </Chan>
</User>

Now we just copy the whole everything between <user> and </user>, to add a new user. I will now make a user called myuser2, with the nickname mynick2 etc.

As we see, the password has been encrypted. To generate and encrypted password for the new user (his password will be password2), we need to exit the text editor, and run the command

znc --makepass

This prompts for a password, and returns the the encrypted password to be included in the config-file.

Having copied the <user>-part and inserted the new password, the znc.conf file looks like this:


// WARNING
//
// Do NOT edit this file while ZNC is running!
// Use webadmin or *admin instead.
//
// Buf if you feel risky, you might want to read help on /znc saveconfig and /znc rehash.
// Also check http://en.znc.in/wiki/Configuration

Listener   = 65000
LoadModule = partyline

<User myusername>
  Pass       = sha256#8947e2b4df7bde63624258d83dea6a35004ee8b7693ec64dd941e2401bbda08e#4sCV(V3I9kP,/Qw5t(Z8#
  Admin      = true
  Nick       = mynick
  AltNick    = mynick_
  Ident      = mynick
  RealName   = My Real Name
  Buffer     = 5000
  KeepBuffer = true
  ChanModes  = +stn


  Server     = irc.freenode.net 6667 

  <Chan #archlinux>
  </Chan>
</User>
<User myusername2>
  Pass = sha256#c1e227a2d69d258630726e161b7d9d2c0edf47b92ddfe96ed10face08fe2ac2a#s(P;8wZ-w+s8)WuOCVNF#
  Admin      = true
  Nick       = mynick2
  AltNick    = mynick2_
  Ident      = mynick2
  RealName   = My Real Name2
  Buffer     = 5000
  KeepBuffer = true
  ChanModes  = +stn


  Server     = irc.freenode.net 6667 

  <Chan #archlinux>
  </Chan>
</User>

Now we can start the znc service, and connect to our channel through any IRC client (supporting SSL). Start znc by running:

znc

Connencting to IRC channel through ZNC using IRSSI:

For demonstrations, lets say the domain, where the znc-server is hosted, is irc.mydomain.com. Then you connect to your znc in IRSSI using the following command:

/connect -ssl irc.mydomain.com 65000 myusername:mypassword

To automatically use these informations as you start IRSSI, execute the following line in IRSSI:

/server add -auto -network mynetwork -ssl irc.mydomain.com 65000 myuser:mypassword

You can of course use any client you want, but remember to use SSL for the connection. Also a weak certificate may be used, so just ignore this.

simple_away

Additionally, you can load the module simple_away. This marks the user away when he/she logs out of znc, by closing the client for example. To add the module, the following line needs to be added inside every <user>, who needs the module (it could be the line before "Server = ..." above, in the example above).

  LoadModule = simple_away -timer 0

The 0 after timer is the number of second from the user logs out 'till the /away-command is executed.