Class Notes
(811,705)

Canada
(494,883)

University of Waterloo
(18,198)

Computer Science
(752)

CS 137
(16)

Andrew Morton
(15)

Lecture 11

# Programming Lecture 11 & 12.docx

Unlock Document

Computer Science

CS 137

Andrew Morton

Fall

Description

Programming Lecture 11 & 12
October 23, 2012
Complex Numbers
x + yi
o x is real
o y is imaginary
i = sqrt(-1)
2
i = -1
x + 1 = 0
x = -1
Addition
o (x + yi) + (w + zi) = (x + w) + (y + z)i
Multiplication
o (x + yi)(w + zi) = xw – yz + (xz + yw)i
Roll-your-own in C89
cplex.h:
#ifndef CPLEX_H
#define CPLEX_H
typedef struct
{
double r; //real
double i; //imaginary
}cplex;
cplex cplexConstruct (double r, double i);
cplex cplexAdd(cplex a, cplex b);
cplex cplexMult(cplex a, cplex b);
void cplexPrint(cplex a);
#endif
cplex.c:
#include
#include “cplex.h”
cplex cplexConstruct (double r, double i)
{
cplex c;
c.r=r;
c.i=I;
//OR cplex c = {r, i};
return c;
} cplex cplexAdd (cplex a, cplex b)
{
cplex c = {a.r+b.r, a.i+b.i};
return c;
}
cplex cplexMult (cplex a, cplex b)
{
cplex c = {a.r*b.r-a.i*b.i, a.r*b.i+a.i*b.r};
return c;
}
void cplexPrint (cplex a)
{
printf(“%g + %gi\n”, a.r, a.i); //prints “1 + 2i” when a = 1.0 + 2.0i
}
main.c:
#include “cplex.h”
int main()
{
cplex a, b, c;
a = cplexConstruct (1,2);
b = cplexConstruct (3,4);
c = cplexAdd(a,b);
cplexPrint(c); // 4 + 6i
c = cpleMult(a,b);
cplexPrint(c); // -5 + 10i
return 0;
}
main.c in C99:
#include
#include
int main ()
{
double complex a = 1 + 2*I;
double complex c = 3 + 4*I;
double complex c = a+b;
printf(“%g+%gi\n”, creal(c), cimag(c));
return 0;
} Website Crawling
How does Google read your mind?
o Structure and content of web pages
o User behavior
o “Quality” of web pages (static rank)
Early version: PageRank
o Random Surfer Model
Pretend you’re “surfing”
At each step, you either:
Follow a link
o Probability d
Jump to a “random” page
o Probability 1-d
The PageRank of page Q, R(Q) if the probability you’re on page Q after surfing for a
long time
Computing
PageRank – R(Q)
o Out(P)
He number of links out of P
o N pages in total
o R(Q) = (1-d)/N + d ∑ )) )

More
Less
Related notes for CS 137