18 Mayıs 2018 Cuma

HASH INTERFACE

type Hash interface {
        // Write (via the embedded io.Writer interface) adds more data to the running hash.
        // It never returns an error.
        io.Writer

        // Sum appends the current hash to b and returns the resulting slice.
        // It does not change the underlying hash state.
        Sum(b []byte) []byte

        // Reset resets the Hash to its initial state.
        Reset()

        // Size returns the number of bytes Sum will return.
        Size() int

        // BlockSize returns the hash's underlying block size.
        // The Write method must be able to accept any amount
        // of data, but it may operate more efficiently if all writes
        // are a multiple of the block size.
        BlockSize() int
}

Goruntu Isleme- QR DECOMPOSİTİON

q:
 [[-0.857143  0.394286  0.331429]
 [-0.428571 -0.902857 -0.034286]
 [ 0.285714 -0.171429  0.942857]]
r:
 [[ -14.  -21.   14.]
 [   0. -175.   70.]
 [   0.    0.  -35.]]

polyfit:
 [ 1.  2.  3.]

Goruntu Isleme -QR DECOMPOSİTİON

#!/usr/bin/env python3
 
import numpy as np
 
def qr(A):
    m, n = A.shape
    Q = np.eye(m)
    for i in range(n - (m == n)):
        H = np.eye(m)
        H[i:, i:] = make_householder(A[i:, i])
        Q = np.dot(Q, H)
        A = np.dot(H, A)
    return Q, A
 
def make_householder(a):
    v = a / (a[0] + np.copysign(np.linalg.norm(a), a[0]))
    v[0] = 1
    H = np.eye(a.shape[0])
    H -= (2 / np.dot(v, v)) * np.dot(v[:, None], v[None, :])
    return H
 
# task 1: show qr decomp of wp example
a = np.array(((
    (12, -51,   4),
    ( 6, 167, -68),
    (-4,  24, -41),
)))
 
q, r = qr(a)
print('q:\n', q.round(6))
print('r:\n', r.round(6))
 
# task 2: use qr decomp for polynomial regression example
def polyfit(x, y, n):
    return lsqr(x[:, None]**np.arange(n + 1), y.T)
 
def lsqr(a, b):
    q, r = qr(a)
    _, n = r.shape
    return np.linalg.solve(r[:n, :], np.dot(q.T, b)[:n])
 
x = np.array((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
y = np.array((1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321))
 
print('\npolyfit:\n', polyfit(x, y, 2))

12 Nisan 2018 Perşembe

GÖRÜNTÜ İŞLEME ÖDEV*


#!/usr/bin/python
# -*- coding: utf-8 -*-

#-----------------------------------------------------------------------------
# husonet
# Hüseyin Özdemir
# 03.03.2011
# 2 adet tanımlanmış eleman sayısı aynı olan matris çarpımını yapar
#------------------------------------------------------------------------------

if __name__ == '__main__':
    matris1      = [[1, 2, 3],[4, 5, 6]]
    matris2      = [[1, 2, 3],[4, 5, 6]]
    carpimMatris = []
    for mat2 in matris2:
        for eleman2 in mat2:
            for mat1 in matris1:
                for eleman1 in mat1:
                    carpimMatris.append(eleman1 * eleman2)
    print carpimMatris
 

>>> from numpy import *    # Numpy kütüphanesini ön alan adı eki olmadan ekliyoruz
>>> print(identity(3))     # birim köşegen matris
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
>>> print(eye(3))          # birim köşegen matris
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
>>> print(eye(3, k=1))     # köşegenin bir üst çapraza birim vektör ekleme
[[ 0.  1.  0.]
 [ 0.  0.  1.]
 [ 0.  0.  0.]]
>>> print(eye(3, k=-2))    # köşegenin iki alt çapraza birim vektör ekleme
[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 1.  0.  0.]]
 
>>> print(diag([2,3,4], k=0))       # [2,3,4] dizisinden köşegen matris
[[2 0 0]
 [0 3 0]
 [0 0 4]]
>>> print(diag([2,3,4], k=1))       # aynı diziden bir üst köşegen matris
[[0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]
 [0 0 0 0]]
>>>
>>> # üçlü bant matris oluşturma
>>> print(diag(ones(4),k=-1) + diag(2*ones(5),k=0) + diag(ones(4), k=1))
[[ 2.  1.  0.  0.  0.]
 [ 1.  2.  1.  0.  0.]
 [ 0.  1.  2.  1.  0.]
 [ 0.  0.  1.  2.  1.]
 [ 0.  0.  0.  1.  2.]]
  


>>> A = ones((4,4))    # 1'lerden oluşan matris
>>> x = ones(4)        # 1'lerden oluşan vektör
>>> b = A*x            # matrisin her bir sütununun x ile vektörel çarpımı
>>> print(b)
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
>>> b = dot(A,x)      # lineer cebir çarpımı
>>> print(b)
[ 4.  4.  4.  4.]
 
>>> A = random.random((4,4))     # rastgele kare A matrisi
>>> print(A)
[[ 0.69769186  0.79776825  0.09984681  0.21271922]
 [ 0.05979394  0.72758335  0.0570817   0.59825989]
 [ 0.61698744  0.29374527  0.82004994  0.41084264]
 [ 0.02961924  0.86964469  0.63311203  0.82975882]]
>>> b = random.random(4)         # rastgele b vektörü
>>> print(b)
[ 0.20801453  0.81169016  0.84603751  0.74985329]<
>>>
>>> x = dot(linalg.inv(A), b)    # Ax=b çözümü olarak x=A^-1*b
>>> print(dot(A,x))              # bulduğumuz çözümün sağlamasını yapıyoruz (Ax=b)
[ 0.20801453  0.81169016  0.84603751  0.74985329]
 

 

6 Nisan 2018 Cuma

Goruntu Isleme Odev

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
static int sum =0;
struct Node{
    int data;
    struct Node *left, *right;
};
struct Node *addNode(struct Node *root,int data){
    if(root==NULL){
            struct Node *tmp=(struct Node *)malloc(sizeof(struct Node));
    tmp->data=Null;
    tmp->left=Null;
    tmp->right=NULL;
    return tmp;  
       
    }
    if(root->data<data){
        root->right=addNode(root->right,data);
    }
    else{
        root->left=addNode(root->left,data);
    }
    return root;
}
void print(struct Node *root){
    if(root){
        printf("%d",root->data);
        print(root->left);
         print(root->left);
       
    }
}
int minumum(struct Node *root){
    if(root->left==Null){
        return root->data;
    }
    return minumum(root->left);
}
int max(struct Node *root){
    if(root->right==Null){
        return root->data;
    }
    return max(root->right);
}

5 Nisan 2018 Perşembe

GORUNTU ISLEME CUMA GUNU ODEVI

class Tree
{
private TreeNode root;
int h1 = 0,h2=0;
public Tree()
{ 
root = null; 
}
public TreeNode getRoot()
{ 
return root; 
}



int dugumsayisi(TreeNode p)    //Ağaçtaki node sayısını bulur
{
if(p==null)return 0;
if(p.leftChild!=null)
{
h1=dugumsayisi(p.leftChild);
}
if(p.rightChild!=null)
{
h2=dugumsayisi(p.rightChild);
}
return(max(h1,h2)+1);
}


private int max(int h1, int h2) {
if(h1>h2)return h1;
return h2;
}


public void preOrder(TreeNode localRoot)    //Önce kök sonra left child
{                                           //Son olarak right child taranır
if(localRoot!=null)
{
localRoot.displayNode();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}

public void inOrder(TreeNode localRoot)     //Önce left child sonra kök
{                                           //Son olarak right child taranır
if(localRoot!=null)                       //Aynı zamanda ağacı sıralı bir şekilde yazar
{
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
}
}

public void postOrder(TreeNode localRoot)      //Önce left child
{                                              //Sonra right child
if(localRoot!=null)                          //Son olarak kök taranır
{   
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
}
}


public void insert(int yenidata)            //Ağaca eleman ekleme
{
TreeNode yeniNode = new TreeNode();
yeniNode.data = yenidata;
if(root==null)
root = yeniNode;
else
{
TreeNode current = root;
TreeNode parent;
while(true)
{
parent = current;
if(yenidata < current.data)
{
current = current.leftChild;
if(current==null)
{
parent.leftChild=yeniNode;
return;
}
} 
else
{
current = current.rightChild;
if(current==null)
{
parent.rightChild=yeniNode;
return;
}
}
}   
}
}



int derinlik(TreeNode root)              //Ağacın derinliğini hesaplama
{ 
if (root==null) 
{ 
return -1; 
} 
else { 
int lDepth = derinlik(root.getLeftChild()); 
int rDepth = derinlik(root.getRightChild());
if (lDepth > rDepth)
return(lDepth+1); 
else return(rDepth+1); 
} 
}




public int minValue() { 
return( minValue(root) ); 
} 

private int minValue(TreeNode node) { 
TreeNode current = node; 
while (current.getLeftChild() != null) { 
current = current.getLeftChild(); 
}
return(current.getData()); 
}





public int maxValue() { 
return( maxValue(root) ); 
} 


private int maxValue(TreeNode node) { 
TreeNode current = node; 
while (current.getRightChild() != null) { 
current = current.getRightChild(); 
}
return(current.getData()); 
}






static void lookup(TreeNode node, int target)    //Ağaçta arama
{ 
if (node == null) 
{ 
return; 
} 
else 
{ 
if (target == node.getData()) 
System.out.println(node.getData()+"bulundu"); 
else { 
if (target < node.getData()) lookup(node.getLeftChild(), target); 
else lookup(node.getRightChild(), target); 
} 
} 
}


public boolean isBST() {        //Ağaç ikili ağaç mı?
return(isBST(root)); 
}

private boolean isBST(TreeNode node) 
{ 
if (node==null) return(true);
if (node.leftChild!=null && maxValue(node.leftChild) > node.data) 
return(false); 
if (node.rightChild!=null && minValue(node.rightChild) <= node.data) 
return(false);
return( isBST(node.leftChild) && isBST(node.rightChild) ); 
} 


public boolean sameTree(Tree other) {      //İki ağaç eşit mi?
return( sameTree(root, other.root) ); 
}

boolean sameTree(TreeNode a, TreeNode b) 
{ 
if (a==null && b==null) 
return(true);
else if (a!=null && b!=null) 
{ 
return( a.data == b.data && 
sameTree(a.leftChild, b.leftChild) && 
sameTree(a.rightChild, b.rightChild) 
); 
} 
else return(false); 
} 

} 

30 Mart 2018 Cuma

29.03.2018 cuma Derste Yazılan Kodlar

from random import randint
def create_A_vector(size):
    my_vector=[]
    for i in range(size):
        my_vector.append(randint(0,9))
       
        return my_vector
my_vector_1=create_A_vector(5)
my_vector_1

def create_a_matrix(m,n):
    my_matrix=[]
    for i in range(m):
       my_matrix.append(create_A_vector(n))
    return my_matrix
a=create_a_matrix(2,3)
a
def matrix_multiplication(a,b):
    m=len(a)
    n=len(a[0])
    p=len(b[0])
   
    my_matrix=create_a_matrix(m,p)
    for i in range(m):
        for j in range(p):
            vector_1=a[i]
            vector_2=[i[j] for i in b]
            my_matrix[i][j]=vector_inner_product(vector_1,vector_2)
    return (my_matrix,m*n*p)
a=create_a_matrix(2,3)
b=create_a_matrix(3,10)
c=matrix_multiplication(a,b)
print(c)
[i[0] for i in a]
len(a),len(a[0])
def vector_inner_product(v1,v2):
    if(len(v1)!=len(v2)):
        print("Vektorler ayni boyutta olmali")
        return
    result=0
    for i in range(len(v1)):
        result =result*v1[i]*v2[i]
    return result
my_vector_1=create_A_vector(5)
my_vector_2=create_A_vector(5)
print(my_vector_1,my_vector_2)
vector_product=vector_inner_product(my_vector_1,my_vector_2)
vector_product
a_1=create_a_matrix(2,3)
a_2=create_a_matrix(10,1000)
a_3=create_a_matrix(1000,1)
a_4=create_a_matrix(1,5)
a_5=create_a_matrix(5,3)

islem_sayisi=0

r_1=matrix_multiplication(a_1,a_2)
islem_sayisi=islem_sayisi+r_1[1]

r_1=matrix_multiplication(r_1[0],a_3)
islem_sayisi=islem_sayisi+r_1[1]

r_1=matrix_multiplication(r_1[0],a_4)
islem_sayisi=islem_sayisi+r_1[1]

r_1=matrix_multiplication(r_1[0],a_5)
islem_sayisi=islem_sayisi+r_1[1]

print(r_1,"Toplam islem sayisi : ",islem_sayisi)