Monthly Archives: July 2022

程序代写 #include – cscodehelp代写

#include
#include

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

Copyright By cscodehelp代写 加微信 cscodehelp

thread_func (void *ptr)
for (i = 0; i < 10; i++) pthread_mutex_lock (&mutex1); printf ("%d", *(int*)ptr); pthread_mutex_unlock (&mutex1); system("sleep 0.1"); pthread_t t1, t2; int t_id1, t_id2, a=1, b=2; t_id1 = pthread_create (&t1, NULL, (void *) &thread_func, &a); t_id2 = pthread_create (&t2, NULL, (void *) &thread_func, &b); pthread_join (t1, NULL); pthread_join (t2, NULL); 程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

计算机代写 EEE8087 1 – cscodehelp代写

ACMs − preliminaries
Dr Fei Xia and Dr Alex Bystrov

Prerequisites

Copyright By cscodehelp代写 加微信 cscodehelp

• Petri nets
• State-transition diagrams – Reachabilitygraphs
• Basic understanding of flip-flops, latches, registers and their operations
• Basic understanding of how clock signals are used in computing hardware
26/10/20 ACM preliminaries, EEE8087 1

ACM resources
• on “atomic register” and on “asynchronous communication mechanisms”
– Google,IEEExplore,etc.
• Research papers on this subject by the μSystems
Research Group, Newcastle University
– BrowseourpublicationsintheComfortandCoherent projects from
http://async.org.uk/comfort/publications.html and http://async.org.uk/coherent/coherent_publications.html
26/10/20 ACM preliminaries, EEE8087 2

Asynchronous Data Communications
• A complex system may include a number of different digital sub-systems, e.g. computers
• A major problem is how to transfer data from one sub-system to another
ACM preliminaries, EEE8087 3
Itboilsdowntodatacommunicationsbetweentwo processes, each running on a different digital computation device, e.g. computer
Weviewtheseprocessesasa‘writer’anda‘reader’ and the goal is to transfer data from the writer to the reader correctly and efficiently

Asynchronous Data Communications • Schematic diagram of the data transfer
Computer 1 Writer
Computer 2 Reader
26/10/20 ACM preliminaries, EEE8087 4

Fundamental Problem
• Synchronization
– Differentcomputerstendtoberunondifferentclocks
– Butatthebasiclevel,abitofdataissent/received through the smallest memory, i.e. flip-flops or latches
Computer 2
Computer 1
26/10/20 ACM preliminaries, EEE8087

Fundamental Problem
• Synchronization
– Thelatchesinvolvedneedtobeonthesameclock
– This‘sameclock’needstobetheclockofComputer1 or Computer 2 but these two are not the same!
Computer 2
Computer 1
26/10/20 ACM preliminaries, EEE8087

Synchronization for data transfer • Synchronizers
– Devicesthatmakethetwosidesofthedatatransfer (writer and reader) run on effectively the same clock
– Losingdatacorrectness
• Problems
ACM preliminaries, EEE8087 7
Metastabilitymeansthatfor100%correctnessyou need waiting of unbounded time
Waitingonlyforboundedtimeyouhavenon-zero probabilities of data errors

• Waiting, if unbounded, violates real-time requirements
• Insert a buffer between reader and writer so either side can move on without waiting for the other side?
• Let’s look at the Petri net model of an n-space buffer for asynchronous data communication
26/10/20 ACM preliminaries, EEE8087 8

Typical n-space buffers
• First in first out (FIFO)
– Thisisthemostcommonbufferbetweentwo different digital devices
• Last in first out (LIFO)
– Alsoknownasastack,thisislesscommonbetween
two different digital devices
• Random access memory (RAM)
– Generallyknownasabag,ifaccessistrulyrandom there is very little real use for it
26/10/20 ACM preliminaries, EEE8087 9

Petri net model of a FIFO buffer • FIFO with n spaces between writer and reader
Writer Reader
26/10/20 ACM preliminaries, EEE8087 10

Each space may be empty (E) or full (F)
• The entire buffer may be empty (reader waits) or full (writer waits)
Writer Reader
26/10/20 ACM preliminaries, EEE8087 11

• Asynchronous communication mechanism between reader and writer
Writer Reader
26/10/20 ACM preliminaries, EEE8087 12

• For fully asynchronous data communication
• No waiting by either writer or reader, single-space buffer
• Wide usage in real-time systems
– Example: public clocks
– ‘Writer’ is the clock’s internal mechanism
– ‘Readers’ are people looking at the clock
– If nobody looks at the clock during an update cycle, it updates anyway – the ‘lost’ value is not used
– If people look at the clock during an update cycle, the ‘data’ stays the same until the next update
– If someone reads the clock multiple times during an update cycle, they read the same value – the same value is re-used
26/10/20 ACM preliminaries, EEE8087

Full reader/writer asynchrony
• Known by a number of different names, but the same thing conceptually
– : Atomic register https://lamport.azurewebsites.net/pubs/interprocess.ps
– : Pool (four-slot ACM) https://ieeexplore.ieee.org/document/41349
• Writer updates value (overwriting)
• Reader does not modify value (re-reading)
• Buffer always contains one valid data item
– Initialized with valid data value before a run
• Synchronized to the writer when writing, to the reader when reading
– But how to accommodate simultaneous reading/writing?
26/10/20 ACM preliminaries, EEE8087 14

ACM preliminaries recap • NoACM
– Fullysynchronizeddatatransfer–readerandwritermust be on the same clock during data transfer and reading and writing happens on the same data at the same time
• Traditional buffers (FIFO etc.)
– Someasynchronyallowedfordatatransfer–readerand writer usually do not access the same data item at the same time
– Waitingcannotbefullyavoided
• Fully asynchronous ACM
– Nowaitingbyeitherside,dataalwaysvalid
26/10/20 ACM preliminaries, EEE8087 15

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

CS代写 COMP 251 – Midterm Page 3 of 10 Fall 2021 – cscodehelp代写

Short answers
1. True or False? Circle your answers. No justification. Wrong answers will receive a penalty of -1.
(a) (2 points) Performing one rotation always preserves the AVL property. A. True B. False
(b) (2 points) In a red-black trees, at most half of the nodes in a path from the root to a leaf are red. A. True B. False

Copyright By cscodehelp代写 加微信 cscodehelp

(c) (2 points) The order of the vertices visited by the depth-first search algorithm (DFS) algorithm is always strictly different than the order of the vertices returned by the breadth-first search algo- rithm (BFS) algorithm.
A. True B. False
(d) (2 points) Let P (n) be a property over a variable n. We want to prove by induction that P (n) is true for all n ≥ n0. Assume the base case P(n0) is true. Then, for the inductive case, only assuming that P (n − 1) is true is always sufficient to prove that P (n) is true too.
A. True B. False
(e) (2 points) If f(n) is O(g(n)) then g(n) is O(f(n)). A. True B. False
(f) (2 points) We implement hash tables using the open addressing technique to solve conflicts. In this implementation, the load factor α cannot exceed 1.
A. True B. False
(g) (2 points) We run the depth-first search algorithm (DFS) on a graph G and identify a back edge. Thus, G has at least one cycle.
A. True B. False
(h) (2 points) Given a partition of the vertices of a weighted undirected graph, the cut has one and only one light edge.
A. True B. False
(i) (2 points) We run the Dijkstra’s algorithm on a graph with a negative-weight cycle. Then, the algorithm could not terminate.
A. True B. False
(j) (2 points) A bipartite graph has no cycle. A. True B. False
COMP 251 – Midterm Page 3 of 10 Fall 2021

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

留学生作业代写 FIT5202 Big Data Processing – cscodehelp代写

Session 09
FIT5202 Big Data Processing
Data Streaming using Apache Kafka and 09 Agenda
• Session 08 Review

Copyright By cscodehelp代写 加微信 cscodehelp

• Implicit vs Explicit Data
• Matrix Factorization
• Collaborative Filtering with ALS
• Streaming using Apache Kafka
• Visualizing in real-time
• Use case : Click stream visualization
• Demo : word count
• Lab Task : Click Stream Analysis and Visualization

Kafka Use Case (Traffic Data Monitoring)
https://www.infoq.com/articles/traffic-data-monitoring- iot-kafka-and-spark-streaming/

What is Apache Kafka?
• Publish-subscribe messaging system
• Enables distributed applications
• Brokers utilize Apache ZooKeeper for management
and coordination of the cluster
• Each broker instance is capable of handling read
and write quantities reaching to the hundreds of thousands each second (and terabytes of messages) without any impact on performance.
https://www.cloudkarafka.com/blog/2016-11-30-part1-kafka-for-beginners-what-is-apache-kafka.html https://www.instaclustr.com/apache-kafka-architecture/

DEMO Kafka Implementation Scenarios for Lab Visualize in Real-time
Aggregation + Visualization
Week8-Topic
Multiple graphs, annotating interesting points
Rolling Mean + Visualization
Real-time visualization Multiple aggregations Multiple graphs
Producer (Multiple Producers)
Week8-Topic1

and Consumer Properties
▪ KafkaProducer
• Bootstrap_servers • Value_serializer
• Api_version
▪ KafkaConsumer
• Consumer_timeout_ms • Auto_offset_reset
• Bootstrap_servers
• Value_deserializer
• Api_version
https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html

Lab Task for .csv
Real time visualization
Total clicks/ Total impressions
clickstream

To be covered in Session 10 Lecture

DEMO Streaming Word Count Demo
words/sentences

Socket : 9999

Lab Task for Streaming Clickstream.csv
clickstream
Real time visualization
Clicks per minute

Thank You!
See you next week.

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

程序代写 2. Kafka_Producer – cscodehelp代写

2. Kafka_Producer

Kafka Producer¶
Produces Current DateTime stamp with a random count continously unless stopped.

Copyright By cscodehelp代写 加微信 cscodehelp

Produces to the topic Week8-Stream

# import statements
from time import sleep
from json import dumps
from kafka import KafkaProducer
import random
import datetime as dt

def publish_message(producer_instance, topic_name, key, value):
key_bytes = bytes(key, encoding=’utf-8′)
value_bytes = bytes(value, encoding=’utf-8′)
producer_instance.send(topic_name, key=key_bytes, value=value_bytes)
producer_instance.flush()
print(‘Message published successfully. Data: ‘ + str(data))
except Exception as ex:
print(‘Exception in publishing message.’)
print(str(ex))

def connect_kafka_producer():
_producer = None
_producer = KafkaProducer(bootstrap_servers=[‘localhost:9092’],
api_version=(0, 10))
except Exception as ex:
print(‘Exception while connecting Kafka.’)
print(str(ex))
return _producer

if __name__ == ‘__main__’:

topic = ‘Week9-Topic’

print(‘Publishing records..’)
producer = connect_kafka_producer()

while True:
data = str(dt.datetime.now().strftime(“%X”)) + ‘, ‘ + str(random.randrange(0,100))
publish_message(producer, topic, ‘parsed’, data)

Publishing records..
Message published successfully. Data: 14:51:05, 90
Message published successfully. Data: 14:51:06, 96
Message published successfully. Data: 14:51:07, 66
Message published successfully. Data: 14:51:08, 70
Message published successfully. Data: 14:51:09, 33
Message published successfully. Data: 14:51:10, 98
Message published successfully. Data: 14:51:11, 39
Message published successfully. Data: 14:51:12, 32
Message published successfully. Data: 14:51:13, 14
Message published successfully. Data: 14:51:14, 93
Message published successfully. Data: 14:51:15, 27
Message published successfully. Data: 14:51:16, 34
Message published successfully. Data: 14:51:17, 92
Message published successfully. Data: 14:51:18, 43
Message published successfully. Data: 14:51:19, 79
Message published successfully. Data: 14:51:20, 42
Message published successfully. Data: 14:51:21, 80
Message published successfully. Data: 14:51:22, 83
Message published successfully. Data: 14:51:23, 78
Message published successfully. Data: 14:51:24, 75
Message published successfully. Data: 14:51:25, 59
Message published successfully. Data: 14:51:26, 43
Message published successfully. Data: 14:51:27, 80
Message published successfully. Data: 14:51:28, 19
Message published successfully. Data: 14:51:29, 86
Message published successfully. Data: 14:51:30, 5
Message published successfully. Data: 14:51:31, 19
Message published successfully. Data: 14:51:32, 60
Message published successfully. Data: 14:51:33, 73
Message published successfully. Data: 14:51:34, 10
Message published successfully. Data: 14:51:35, 8
Message published successfully. Data: 14:51:36, 35
Message published successfully. Data: 14:51:37, 1
Message published successfully. Data: 14:51:38, 88
Message published successfully. Data: 14:51:39, 20
Message published successfully. Data: 14:51:40, 38
Message published successfully. Data: 14:51:41, 83
Message published successfully. Data: 14:51:42, 17
Message published successfully. Data: 14:51:43, 12
Message published successfully. Data: 14:51:44, 57
Message published successfully. Data: 14:51:45, 36
Message published successfully. Data: 14:51:47, 75
Message published successfully. Data: 14:51:48, 76
Message published successfully. Data: 14:51:49, 32
Message published successfully. Data: 14:51:50, 47
Message published successfully. Data: 14:51:51, 22
Message published successfully. Data: 14:51:52, 12
Message published successfully. Data: 14:51:53, 60
Message published successfully. Data: 14:51:54, 85
Message published successfully. Data: 14:51:55, 95
Message published successfully. Data: 14:51:56, 61
Message published successfully. Data: 14:51:57, 87
Message published successfully. Data: 14:51:58, 1
Message published successfully. Data: 14:52:00, 90
Message published successfully. Data: 14:52:01, 38
Message published successfully. Data: 14:52:02, 30
Message published successfully. Data: 14:52:03, 80
Message published successfully. Data: 14:52:04, 42
Message published successfully. Data: 14:52:05, 32
Message published successfully. Data: 14:52:06, 35
Message published successfully. Data: 14:52:07, 0
Message published successfully. Data: 14:52:08, 61
Message published successfully. Data: 14:52:09, 49
Message published successfully. Data: 14:52:10, 1
Message published successfully. Data: 14:52:11, 46
Message published successfully. Data: 14:52:13, 2
Message published successfully. Data: 14:52:14, 46
Message published successfully. Data: 14:52:15, 80
Message published successfully. Data: 14:52:16, 50
Message published successfully. Data: 14:52:18, 93
Message published successfully. Data: 14:52:19, 53
Message published successfully. Data: 14:52:20, 51
Message published successfully. Data: 14:52:21, 34
Message published successfully. Data: 14:52:22, 81
Message published successfully. Data: 14:52:23, 22
Message published successfully. Data: 14:52:24, 84
Message published successfully. Data: 14:52:25, 4
Message published successfully. Data: 14:52:26, 34
Message published successfully. Data: 14:52:27, 36
Message published successfully. Data: 14:52:28, 41
Message published successfully. Data: 14:52:29, 88
Message published successfully. Data: 14:52:30, 90
Message published successfully. Data: 14:52:31, 22
Message published successfully. Data: 14:52:32, 99
Message published successfully. Data: 14:52:33, 74
Message published successfully. Data: 14:52:34, 22
Message published successfully. Data: 14:52:35, 25
Message published successfully. Data: 14:52:36, 93
Message published successfully. Data: 14:52:37, 72
Message published successfully. Data: 14:52:38, 30
Message published successfully. Data: 14:52:39, 63
Message published successfully. Data: 14:52:40, 90
Message published successfully. Data: 14:52:41, 12
Message published successfully. Data: 14:52:42, 23
Message published successfully. Data: 14:52:43, 74
Message published successfully. Data: 14:52:44, 28
Message published successfully. Data: 14:52:45, 21
Message published successfully. Data: 14:52:46, 27
Message published successfully. Data: 14:52:47, 55
Message published successfully. Data: 14:52:48, 40
Message published successfully. Data: 14:52:49, 29
Message published successfully. Data: 14:52:50, 2
Message published successfully. Data: 14:52:51, 63
Message published successfully. Data: 14:52:52, 25
Message published successfully. Data: 14:52:53, 21
Message published successfully. Data: 14:52:54, 16
Message published successfully. Data: 14:52:55, 48
Message published successfully. Data: 14:52:56, 1
Message published successfully. Data: 14:52:57, 83
Message published successfully. Data: 14:52:58, 91
Message published successfully. Data: 14:52:59, 49
Message published successfully. Data: 14:53:00, 74
Message published successfully. Data: 14:53:01, 24
Message published successfully. Data: 14:53:02, 69
Message published successfully. Data: 14:53:03, 75
Message published successfully. Data: 14:53:04, 93
Message published successfully. Data: 14:53:05, 5
Message published successfully. Data: 14:53:06, 48
Message published successfully. Data: 14:53:07, 94
Message published successfully. Data: 14:53:08, 6
Message published successfully. Data: 14:53:09, 31
Message published successfully. Data: 14:53:10, 69
Message published successfully. Data: 14:53:11, 44
Message published successfully. Data: 14:53:12, 89
Message published successfully. Data: 14:53:13, 44
Message published successfully. Data: 14:53:14, 21
Message published successfully. Data: 14:53:15, 0
Message published successfully. Data: 14:53:16, 62
Message published successfully. Data: 14:53:17, 6
Message published successfully. Data: 14:53:18, 64
Message published successfully. Data: 14:53:19, 19
Message published successfully. Data: 14:53:20, 44
Message published successfully. Data: 14:53:21, 2
Message published successfully. Data: 14:53:22, 78
Message published successfully. Data: 14:53:23, 57
Message published successfully. Data: 14:53:24, 35
Message published successfully. Data: 14:53:25, 7
Message published successfully. Data: 14:53:26, 23
Message published successfully. Data: 14:53:27, 95
Message published successfully. Data: 14:53:28, 52
Message published successfully. Data: 14:53:29, 48
Message published successfully. Data: 14:53:30, 46
Message published successfully. Data: 14:53:31, 72
Message published successfully. Data: 14:53:32, 91
Message published successfully. Data: 14:53:33, 97
Message published successfully. Data: 14:53:34, 59
Message published successfully. Data: 14:53:35, 17
Message published successfully. Data: 14:53:36, 75
Message published successfully. Data: 14:53:37, 1
Message published successfully. Data: 14:53:38, 78
Message published successfully. Data: 14:53:40, 10
Message published successfully. Data: 14:53:41, 88
Message published successfully. Data: 14:53:42, 27
Message published successfully. Data: 14:53:46, 47
Message published successfully. Data: 14:54:12, 74
Message published successfully. Data: 14:54:13, 74
Message published successfully. Data: 14:54:14, 10
Message published successfully. Data: 14:54:15, 9
Message published successfully. Data: 14:54:16, 86
Message published successfully. Data: 14:54:17, 66
Message published successfully. Data: 14:54:18, 13
Message published successfully. Data: 14:54:19, 72
Message published successfully. Data: 14:54:20, 80
Message published successfully. Data: 14:54:21, 15
Message published successfully. Data: 14:54:22, 39
Message published successfully. Data: 14:54:23, 80
Message published successfully. Data: 14:54:24, 30
Message published successfully. Data: 14:54:25, 73
Message published successfully. Data: 14:54:26, 15
Message published successfully. Data: 14:54:27, 26
Message published successfully. Data: 14:54:28, 29
Message published successfully. Data: 14:54:29, 76
Message published successfully. Data: 14:54:30, 69
Message published successfully. Data: 14:54:31, 1
Message published successfully. Data: 14:54:32, 5
Message published successfully. Data: 14:54:34, 40
Message published successfully. Data: 14:54:35, 90
Message published successfully. Data: 14:54:36, 62
Message published successfully. Data: 14:54:37, 42
Message published successfully. Data: 14:54:38, 32
Message published successfully. Data: 14:54:39, 86
Message published successfully. Data: 14:54:40, 11
Message published successfully. Data: 14:54:41, 50
Message published successfully. Data: 14:54:42, 32
Message published successfully. Data: 14:54:43, 30
Message published successfully. Data: 14:54:44, 2
Message published successfully. Data: 14:54:45, 52
Message published successfully. Data: 14:54:46, 48
Message published successfully. Data: 14:54:47, 75
Message published successfully. Data: 14:54:48, 68
Message published successfully. Data: 14:54:49, 53
Message published successfully. Data: 14:54:50, 32
Message published successfully. Data: 14:54:51, 79
Message published successfully. Data: 14:54:52, 94
Message published successfully. Data: 14:54:53, 95
Message published successfully. Data: 14:54:54, 29
Message published successfully. Data: 14:54:55, 34
Message published successfully. Data: 14:54:56, 29
Message published successfully. Data: 14:54:57, 19
Message published successfully. Data: 14:54:58, 43
Message published successfully. Data: 14:54:59, 93
Message published successfully. Data: 14:55:00, 97
Message published successfully. Data: 14:55:01, 25
Message published successfully. Data: 14:55:02, 66
Message published successfully. Data: 14:55:03, 33
Message published successfully. Data: 14:55:04, 84
Message published successfully. Data: 14:55:05, 71
Message published successfully. Data: 14:55:06, 20
Message published successfully. Data: 14:55:07, 52
Message published successfully. Data: 14:55:08, 20
Message published successfully. Data: 14:55:09, 90
Message published successfully. Data: 14:55:10, 23
Message published successfully. Data: 14:55:11, 88
Message published successfully. Data: 14:55:12, 52
Message published successfully. Data: 14:55:13, 83
Message published successfully. Data: 14:55:14, 55
Message published successfully. Data: 14:55:15, 56
Message published successfully. Data: 14:55:16, 33
Message published successfully. Data: 14:55:17, 29
Message published successfully. Data: 14:55:18, 86
Message published successfully. Data: 14:55:19, 97
Message published successfully. Data: 14:55:20, 9
Message published successfully. Data: 14:55:21, 91
Message published successfully. Data: 14:55:22, 65
Message published successfully. Data: 14:55:23, 37
Message published successfully. Data: 14:55:24, 75
Message published successfully. Data: 14:55:25, 22
Message published successfully. Data: 14:55:26, 60
Message published successfully. Data: 14:55:27, 64
Message published successfully. Data: 14:55:28, 95
Message published successfully. Data: 14:55:29, 65
Message published successfully. Data: 14:55:30, 77
Message published successfully. Data: 14:55:31, 36
Message published successfully. Data: 14:55:32, 44
Message published successfully. Data: 14:55:33, 31
Message published successfully. Data: 14:55:34, 20
Message published successfully. Data: 14:55:35, 51
Message published successfully. Data: 14:55:36, 89
Message published successfully. Data: 14:55:37, 46
Message published successfully. Data: 14:55:38, 39
Message published successfully. Data: 14:55:39, 84
Message published successfully. Data: 14:55:40, 71
Message published successfully. Data: 14:55:41, 42
Message published successfully. Data: 14:55:42, 45
Message published successfully. Data: 14:55:43, 4
Message published successfully. Data: 14:55:44, 62
Message published successfully. Data: 14:55:45, 67
Message published successfully. Data: 14:55:46, 13
Message published successfully. Data: 14:55:47, 26
Message published successfully. Data: 14:55:48, 42
Message published successfully. Data: 14:55:49, 47
Message published successfully. Data: 14:55:50, 12
Message published successfully. Data: 14:55:51, 90
Message published successfully. Data: 14:55:52, 61
Message published successfully. Data: 14:55:53, 63
Message published successfully. Data: 14:55:54, 37
Message published successfully. Data: 14:55:55, 69
Message published successfully. Data: 14:55:56, 51
Message published successfully. Data: 14:55:57, 92
Message published successfully. Data: 14:55:58, 96
Message published successfully. Data: 14:55:59, 48
Message published successfully. Data: 14:56:00, 50
Message published successfully. Data: 14:56:01, 71
Message published successfully. Data: 14:56:02, 96
Message published successfully. Data: 14:56:03, 27
Message published successfully. Data: 14:56:04, 75
Message published successfully. Data: 14:56:05, 22
Message published successfully. Data: 14:56:06, 65
Message published successfully. Data: 14:56:07, 14
Message published successfully. Data: 14:56:08, 79
Message published successfully. Data: 14:56:09, 49
Message published successfully. Data: 14:56:10, 26
Message published successfully. Data: 14:56:11, 39
Message published successfully. Data: 14:56:12, 44
Message published successfully. Data: 14:56:13, 44
Message published successfully. Data: 14:56:14, 36
Message published successfully. Data: 14:56:15, 1
Message published successfully. Data: 14:56:16, 61
Message published successfully. Data: 14:56:17, 71
Message published successfully. Data: 14:56:18, 83
Message published successfully. Data: 14:56:19, 63
Message published successfully. Data: 14:56:20, 87
Message published successfully. Data: 14:56:21, 88
Message published successfully. Data: 14:56:22, 44
Message published successfully. Data: 14:56:23, 51
Message published successfully. Data: 14:56:24, 84
Message published successfully. Data: 14:56:25, 55
Message published successfully. Data: 14:56:26, 30
Message published successfully. Data: 14:56:27, 55
Message published successfully. Data: 14:56:28, 63
Message published successfully. Data: 14:56:29, 65
Message published successfully. Data: 14:56:30, 85
Message published successfully. Data: 14:56:31, 55
Message published successfully. Data: 14:56:32, 64
Message published successfully. Data: 14:56:33, 10
Message published successfully. Data: 14:56:34, 33
Message published successfully. Data: 14:56:35, 27
Message published successfully. Data: 14:56:36, 43
Message published successfully. Data: 14:56:37, 12
Message published successfully. Data: 14:56:38, 28
Message published successfully. Data: 14:56:39, 84
Message published successfully. Data: 14:56:40, 2
Message published successfully. Data: 14:56:41, 9
Message published successfully. Data: 14:56:42, 74
Message published successfully. Data: 14:56:43, 52
Message published successfully. Data: 14:56:44, 13
Message published successfully. Data: 14:56:45, 88
Message published successfully. Data: 14:56:46, 99
Message published successfully. Data: 14:56:47, 99
Message published successfully. Data: 14:56:48, 98
Message published successfully. Data: 14:56:49, 90
Message published successfully. Data: 14:56:50, 28
Message published successfully. Data: 14:56:51, 62
Message published successfully. Data: 14:56:52, 88
Message published successfully. Data: 14:56:53, 38
Message published successfully. Data: 14:56:54, 13
Message published successfully. Data: 14:56:55, 62
Message published successfully. Data: 14:56:56, 14
Message published successfully. Data: 14:56:57, 63
Message published successfully. Data: 14:56:58, 80
Message published successfully. Data: 14:56:59, 54
Message published successfully. Data: 14:57:00, 92
Message published successfully. Data: 14:57:01, 37
Message published successfully. Data: 14:57:02, 73
Message published successfully. Data: 14:57:03, 0
Message published successfully. Data: 14:57:04, 30
Message published successfully. Data: 14:57:05, 76
Message published successfully. Data: 14:57:06, 54
Message published successfully. Data: 14:57:07, 69
Message published successfully. Data: 14:57:08, 47
Message published successfully. Data: 14:57:10, 58
Message published successfully. Data: 14:57:11, 7
Message published successfully. Data: 14:57:12, 89
Message published successfully. Data: 14:57:13, 65
Message published successfully. Data: 14:57:14, 77
Message published successfully. Data: 14:57:15, 94
Message published successfully. Data: 14:57:16, 37
Message published successfully. Data: 14:57:17, 16
Message published successfully. Data: 14:57:18, 26
Message published successfully. Data: 14:57:19, 40
Message published successfully. Data: 14:57:20, 58
Message published successfully. Data: 14:57:21, 51
Message published successfully. Data: 14:57:22, 70
Message published successfully. Data: 14:57:23, 15
Message published successfully. Data: 14:57:24, 61
Message published successfully. Data: 14:57:25, 19
Message published successfully. Data: 14:57:26, 96
Message published successfully. Data: 14:57:27, 44
Message published successfully. Data: 14:57:28, 42

—————————————————————————
KeyboardInterrupt Traceback (most recent call last)
in
39 data = str(dt.datetime.now().strftime(“%X”)) + ‘, ‘ + str(random.randrange(0,100))
40 publish_message(producer, topic, ‘parsed’, data)
—> 41 sleep(1)

KeyboardInterrupt:

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

CS代写 Linear_Equations – cscodehelp代写

Linear_Equations

Systems of Linear Equations¶

Copyright By cscodehelp代写 加微信 cscodehelp

In this notebook we leran various functions for solving and manipulating systems of linear equations.

Initialization¶

import numpy as np
import scipy.linalg as la
import sys

The main linear algebra routines are contained in scipy.linalg. Behind the scenes the calculations are done using BLAS and LAPACK libraries. Depending on how your version was compiled these can be highly optimized and even parallelized.

Some functions also have implementations in NumPy. In general we are going to prefer the versions from scipy.linalg. In practice, both versions should give the same results except in extreme cases, such as dealing with (numerically) singular matrices.
We will represent both vectors and matrices using NumPy arrays. NumPy arrays are not vectors or matrices. A vector or matrix is a special object that has special properties. For example, we know what multiplying a matrix and a vector means, where as multiplying a two dimensional array and a one dimensional array is not uniquely defined. There is a matrix object in NumPy but we will not use those in this notebook. Due to the fact that we are using arrays instead of matrices this means we will need to use the function np.dot when we multiply a matrix and a vector. See below for details.

As always, begin by looking at the documentation. From scipy.linalg we see there are many routines. Here we will focus on some of those from “Basics” and those related to the LU decomposition.

Also as always we should look at the documentation for any and all functions that interest us.

la.lu_factor?

la.lu_solve?

Notice that there are a few functions related to the LU decomposition. We will learn why this is the case below.

Sample linear system¶
Consider a sample linear system of equations
$$ egin{align}
10 x_1 – 7 x_2 + x_3 &= 8, \
-3 x_1 + 2 x_2 + 6 x_3 &= 4, \
5 x_1 – x_2 + 5 x_3 &= 6 .
end{align} $$
We can write this in the form
$$defmat#1{mathsf{#1}}
A x = b, $$
$$ A = left( egin{array}{rrr}
10 & -7 & 1 \
-3 & 2 & 6 \
5 & -1 & 5
end{array}
ight)
quadmathrm{and}quad
b = left( egin{array}{c} 8 \ 4 \ 6 end{array}
ight). $$

We can create these as arrays using np.array. Notice that we force the result to be a floating point array, not an integer array, by making any one of the entries a floating point number. NumPy tries to use the “simplest” data type when it creates an array. There are a few ways to force the type it chooses, this is one way.

A = np.array([ [10, -7, 1.], [-3, 2, 6], [5, -1, 5] ])
b = np.array([8., 4, 6])

Solving the System¶
To solve this system we can, not surprisingly, use solve.

x = la.solve(A, b)

[ 0. -1. 1.]

Verifying the Solution¶
To verify the solution we can directly evaluate $A x$ and compare it to $b$. Of course this only verifies that the solve has worked correctly, it does not verify that we have entered $A$ and $b$ correctly. There are two steps, one is to actually perform the multiplication and the second is to compare the $A x$ to $b$.

Matrix Multiplication¶
To multiply matrices we do not use ‘ * ‘. The usual multiplication is component-wise multiplication. This is not the same as matrix multiplication.

To see this, consider using the usual multiplication:

if (sys.version_info > (3, 0)):
# Python 3 code in this block
print(‘A =’, A)
print(‘x =’, x)
print(‘A*x =’, A*x)
# Python 2 code in this block
print(“A = %s” % np.array_str(A))
print(“x = %s” % np.array_str(x))
print(“A*x = %s” % np.array_str(A*x))

A = [[10. -7. 1.]
[-3. 2. 6.]
[ 5. -1. 5.]]
x = [ 0. -1. 1.]
A*x = [[ 0. 7. 1.]
[-0. -2. 6.]
[ 0. 1. 5.]]

Notice what happened: the first component of $b$ multiplied the first column of $A$, the second component of $b$ multiplied the second column of $A$, and similarly for the third. This is what we mean by component-wise multiplication.

If we want to perform matrix multiplication we need to use a function, np.dot. Consider

if (sys.version_info > (3, 0)):
# Python 3 code in this block
print(‘np.dot(A, x) =’, np.dot(A,x))
# Python 2 code in this block
print(“np.dot(A, x) = %s” % np.array_str(np.dot(A,x)))

np.dot(A, x) = [8. 4. 6.]

Notice that this returns a vector and that vector should be $b$.

Comparing calculations¶
This is a small system so we can easily see that all values are nearly zero. We do not want to check that $A x$ is exactly equal to $b$ (in general this does not work numerically), however, we can check that the values are “close enough”. There are a few ways to do this, here we use np.allclose. (At this point you may be checking the documentation! Here we are using the default settings, but could adjust the tolerances for a more stringent test.)

print(np.dot(A,x)-b)
print(np.allclose(np.dot(A, x), b))

[0. 0. 0.]

Factoring the system¶
The work required to solve the system of equations mostly involves manipulating the matrix, $A$, and then performing the same manipulations on the right hand side of the equations, $b$. We could instead have many right hand sides (a two dimensional array with multiple columns, one for each set of values for which we want to find a solution). This is handled by solve. Alternatively, we can decompose $A$ into pieces that encode the required manipulations using the LU decomposition. The decomposition only needs to be performed once, it can then be applied whenever needed. Finally, for numerical stability we should also pivot the matrix with permutation matrix $P$. LU decomposition with pivoting is provided by scipy.linalg.lu.

(P, L, U) = la.lu(A)

if (sys.version_info > (3, 0)):
# Python 3 code in this block
print(“Permutation matrix:
”, P)
print(“Lower triangular matrix:
”, L)
print(“Upper triangluar matrix:
”, U)
print(“PLU == A?”, np.allclose(np.dot(P,np.dot(L,U)), A))
# Python 2 code in this block
print(“Permutation matrix:
%s” % np.array_str(P))
print(“Lower triangular matrix:
%s” % np.array_str(L))
print(“Upper triangluar matrix:
%s” % np.array_str(U))
print(“PLU == A? %r” % np.allclose(np.dot(P,np.dot(L,U)), A))

Permutation matrix:
[[1. 0. 0.]
[0. 0. 1.]
[0. 1. 0.]]
Lower triangular matrix:
[[ 1. 0. 0. ]
[ 0.5 1. 0. ]
[-0.3 -0.04 1. ]]
Upper triangluar matrix:
[[10. -7. 1. ]
[ 0. 2.5 4.5 ]
[ 0. 0. 6.48]]
PLU == A? True

By default we are given the permutation matrix $P$ and the lower and upper triangular matrices that, when recombined, produce $mat A$. This function is good when we want to see the decomposition in a form easy for us to read. If we want to use the decomposition for solving systems of linear equations the information could be stored in a more efficient form for the computer’s use.

Note that our choice of using generic arrays for storing matrices comes at a cost when we want to multiply many of them together. Here we have had to use nested calls to np.dot. This can be slightly streamlined, but remains tedious. It is just a price we must pay for our choice. In practice it is usually a rather small price.

Getting back to solving a system of equations, we can use scipy.linalg.lu_factor to decompose in a form more useful for the computer.

la.lu_factor(A)

(array([[10. , -7. , 1. ],
[ 0.5 , 2.5 , 4.5 ],
[-0.3 , -0.04, 6.48]]),
array([0, 2, 2], dtype=int32))

Here $L$ and $U$ have been merged into a single matrix (notice in the form above that $L$ had ones along the diagonal so they do not need to be stored) and the permutations are represented by a one dimensional array instead a matrix. This is much more memory efficient, but is also much harder for us to read. Even so, this can be used in scipy.linalg.lu_solve. In fact, the tuple returned here is exactly what needs to be provided as the first argument to that function.

lufactors = la.lu_factor(A)
xlu = la.lu_solve(lufactors, b)
print(“LU solution: %s” % np.array_str(xlu))
print(“Consistent with previous solution? %r” % np.allclose(xlu, x))

LU solution: [ 0. -1. 1.]
Consistent with previous solution? True

If $A=LU$, then $Ax=LUx=b$. Thus, first solve $Ly=b$, for $y$, then solve $Ux=y$ for $x$. Both solutions are found by back-substitution.

With permutation matrix $P$, such that $PA=LU$ the decomposition is sometimes called LUP decomposition, so $LUx=Pb$. In this case the solution is also done in two logical steps: (1) solve the equation $Ly=Pb$ for $y$ and (2) solve the equation $Ux=y$ for $x$.

Slightly Larger System¶
As an example of a slightly larger system and one where we want to find solutions with different right hand sides consider
$$mat A = left( egin{array}{rrrr}
2 & -3 & 1 & 3 \
1 & 4 & -3 & -3 \
5 & 3 & -1 & -1 \
3 & -6 & -3 & 1
end{array}
ight),
vec b_1 = left( egin{array}{r}
-4 \ 1 \ 8 \ -5
end{array}
ight),
vec b_2 = left( egin{array}{r}
-10 \ 0 \ -3 \ -24
end{array}
ight).

A = np.array([ [2., -3, 1, 3], [1, 4, -3, -3], [5, 3, -1, -1], [3, -6, -3, 1]])
b1 = np.array([-4., 1, 8, -5])
b2 = np.array([-10, 9, -3, -24])

We can solve this directly using solve.

print(“Solution for b1: %s” % np.array_str(la.solve(A, b1)))
print(“Solution for b2: %s” % np.array_str(la.solve(A, b2)))

Solution for b1: [ 2. -1. 4. -5.]
Solution for b2: [-2.6875 3.925 -1.56875 2.90625]

Alternatively we can use an LU decomposition. Notice the decomposition is only performed once.

lufactors = la.lu_factor(A)
print(“LU solution for b1: %s” % np.array_str(la.lu_solve(lufactors, b1)))
print(“LU solution for b2: %s” % np.array_str(la.lu_solve(lufactors, b2)))

LU solution for b1: [ 2. -1. 4. -5.]
LU solution for b2: [-2.6875 3.925 -1.56875 2.90625]

Finally, we could have solved for both right hand sides at once. To do this we need to combine b1 and b2. We do this below.

b = np.vstack((b1, b2))

array([[ -4., 1., 8., -5.],
[-10., 9., -3., -24.]])

If we try to use this with solve it fails!

la.solve(A, b)

—————————————————————————
ValueError Traceback (most recent call last)
in
—-> 1 la.solve(A, b)

~anaconda3libsite-packagesscipylinalgasic.py in solve(a, b, sym_pos, lower, overwrite_a, overwrite_b, debug, check_finite, assume_a, transposed)
148 # Last chance to catch 1×1 scalar a and 1-D b arrays
149 if not (n == 1 and b1.size != 0):
–> 150 raise ValueError(‘Input b has to have same number of rows as ‘
151 ‘input a’)

ValueError: Input b has to have same number of rows as input a

We get an incompatible dimensions error because b should have the same number of rows as A, not the same number of columns. This is easy to fix, just take the transpose of b. There is shorthand for doing this as shown below.

print(“Original b:
%s” % np.array_str(b))
print(“Transpose of b:
%s” % np.array_str(b.T))

Original b:
[[ -4. 1. 8. -5.]
[-10. 9. -3. -24.]]
Transpose of b:
[[ -4. -10.]
[ 1. 9.]
[ 8. -3.]
[ -5. -24.]]

With this we find:

print(“scipy.linalg.solve:
%s” % np.array_str(la.solve(A, b.T)))
print(“scipy.linalg.lu_solve:
%s” % np.array_str(la.lu_solve(lufactors, b.T)))

scipy.linalg.solve:
[[ 2. -2.6875 ]
[-1. 3.925 ]
[ 4. -1.56875]
[-5. 2.90625]]
scipy.linalg.lu_solve:
[[ 2. -2.6875 ]
[-1. 3.925 ]
[ 4. -1.56875]
[-5. 2.90625]]

Each column of the result is the solution for each column in the array b.

Note: This notebook is adapted from http://www.phys.cwru.edu/courses/p250/

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com