\documentclass[../../book.tex]{subfiles}
\graphicspath{{\subfix{.}}}
\begin{document}

\chapter{Computers}
\label{Computers}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setcounter{section}{-1}  % Start subsections from 0

\section{A computer is not just a laptop or a rack of servers}
\setcounter{subsection}{-1}  % Start subsections from 0

% Examples of everyday items that are computers or have computers in them
% \begin{itemize}
%     \item Mechanical computing device???
%     \item 4-function calculator
%     \item Microwave
%     \item Cell phone
% \end{itemize}
You are probably used to thinking of computers as the laptop or desktop that you use to write lab reports, analyze data, operate instruments, or look up papers.  While this is certainly true, computers are all around us, most of which run software created using very similar tools to those taught in this book.  The first ``programmable computers" (in other words, computers that can perform different computations depending on instructions from a user) were in fact mechanical, but still functioned overall similarly to those of today.  Many of the objects with which you interact today are computers: microwaves, graphing calculators, and computers all operate fundamentally on the same principles.  So what is a computer?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Computers convert input data into output data}
\setcounter{subsection}{-1}  % Start subsections from 0

At their most basic level, computers can be thought of as the converting of data, as outlined in Figure \ref{fig:computers:dataflow}.  This data can be in the form of inputs (data that the computer needs to solve the problem) and outputs (data that forms a representation of the solution).  This conversion happens in a series of computations, in which new data is created via a series of processes fully determined by the input data.  As users, we can both give the computer commands (e.g., from a keyboard or mouse, by speaking, via email, or any number of other avenues) that instruct it on what we wish it to do, and perceive the results (e.g., on a display, through a printer or speaker, etc.).  The process of receiving commands from the user and translating results into a form that can be perceived in the real world 
%also necessarily involves data and computation, and 
often makes up the bulk of programming.

\begin{figure}[h]
\begin{center}
\includegraphics[width=4in]{ChapTechnical/Computers/Figures/DataFlow.pdf}
\caption{The flow of data in a computer.  Input data of many different types are converted into output data via a process known as ``computation."}
\label{fig:computers:dataflow}
\end{center}
\end{figure}

Computation is the basic problem solving steps you would use to perform any task, like the evaluation of mathematical expressions or the organization of information into categories and groups.  The combination of many of these individual computational steps forms an \textbf{algorithm}, a self-contained set of instructions to perform a specific task.  Thus, data conversion boils down to the application of a series of algorithms to data to transform it in the desired way.  The power of using a computer to do this is largely four-fold: 
\begin{enumerate}
    \item The computer can execute them many orders of magnitude faster than you can.
    \item The computer can work with far more data at a single moment than you can.
    \item The computer does not get bored doing the same thing over and over, and very rarely makes ``mistakes."
    \item It can perform the computation while you are doing something else, freeing up you time for tasks that are not (yet) well-suited to a computer.  
\end{enumerate}
But if computation is the process of transforming data, this raises the question: what is data?

\subsection{What is data?}
\setcounter{subsubsection}{-1}  % Start subsections from 0

You are probably used to thinking of data as an NMR spectrum, a table of experimental results, or the thing you sometimes run out of if you've been streaming video to your phone.  And, in all cases, you are correct. But data is much more general. Data is a representation of information.  More philosophical texts might discuss the underlying link between data and information theory, but here we will stick to the pragmatic (to get you solving chemistry problems as quickly as possible).  For the purpose of this book, data is information that is transferred to and/or from the computer and/or stored in the computer.  The results of your experiments, which you are used to calling ``data,'' is thus one type of data with which we must deal.  However, you will see that from the perspective of a computer program, data is a much broader concept.  

In a computer, every everything is data.  Let's take a simple hand-held calculator as an example.  You might think that the data is the numbers you press, and the computation is the plus or equals buttons that you press.  However, from the calculator's perspective, there is much more data than this.  We can see that this is the case, by conceptualizing the process of data conversion in the calculator as follows:

\begin{enumerate}
    \item The primary data is all of the buttons the user pressed, whether they represent numbers or not.  At first, the calculator does not need to know what those buttons mean, just which ones were pressed in which order.
    \item The first computation required is to convert which button is pressed (the real world) into data in the form of numbers or commands.  Here, pressing ``2" ``+" ``2" ``=" will be converted into two pieces of data representing the two different number \texttt{2}s, a command that the numbers should be added, and a command to execute the calculation.
    \item Once the command to execute the calculation is given, the program must perform the arithmetic and remember the result, in this case (hopefully) \texttt{4}.
    \item The user can not know that \texttt{4} is the result unless the calculator presents it appropriately.  This means, for a simple calculator with a display made up of a series of seven-segment LCD devices, that the calculator must determine which segments should be darkened to create a shape that is interpreted by the user as the numeral ``4."
\end{enumerate}
Note here that pressing the plus button or the equals button is not computation, that is still input (data).  This data tells the calculator that you have requested it to perform a specific arithmetic operation. But it does not `do' that operation. In addition to all of this, there is a significant amount of data that is not obvious in our example. For instance, some data is instructions on \textit{how} to darken the parts of the LCD screen that will represent `4' to the user, but changing voltages in circuts, etc. Most of the data involved in the calculator is focused on user input and output, not the calculation to be done!

\subsection{How computers store and represent data}
\setcounter{subsubsection}{-1}  % Start subsections from 0

Given this diverse array of data that we must consider as programmers, we next have to discuss how the computer actually \textit{remembers} the data.  For reasons that we won't go into detail here, computers operate fundamentally using only two values: often represented as \texttt{0} and \texttt{1}.  This limited range of expression of information has necessitated the development of a complex set of systems for representing all other types of information as a series of these two numbers.  Each digit in the series is called a `bit', and 8 bits make a `byte'.  Generally, the longer the series of 1s and 0s numbers, the more expressive power the system has to represent larger or more complex types of data. However, because 0 and 1 are called `binary' the numbers that are expressed using a series of 0s and 1s are called `binary numbers' and the scheme by which they are translated to and from `regular numbers' is called \cite{binary_encoding}. These systems are described in arbitrarily large detail by a number of sources, so we won't reproduce those efforts here, but we will highlight two systems as basic examples.

\subsubsection{Memory Structure}

In the abstract, you can think of main memory as simply a sea of bits that the computer can use as it sees fit.  This sea is divided into one-byte chunks (1 byte = 8 bits), where each byte has a corresponding ``address'' that the computer uses to access the data in a given chunk or overwrite data that is already there.  Addresses are also binary numbers, typically (but not always) the number of bits used by your computer processor (64-bit is the most common as of this writing).  This number of bits sets a theoretical upper limit on the size of your memory, since you will run out of binary numbers to use as an address after $2^{64}$.  Practically, for most of us, this is not a concern, as that works out to more than 16 million TB!  However, even relatively recently, 32 bit processors were common, with a theoretical limit of 4 GB.

Memory addresses are commonly expressed as \cite{hexadecimal} numbers, where each digit represents four bytes.  So, a 64-bit memory address can be written as \texttt{0x8f397a395dbe06b6}, where \texttt{0x} signifies that we should expect a hexadecimal number and the memory address appears as a 16-digit, 64-bit hexadecimal number.  You may see such numbers flash on your screen when your computer encounters a particularly nasty error.  In principle, these memory address numbers in the error message carry information that could be used to debug the error; in practice, they carry only sadness.

\subsubsection{Storing Data in Memory}
\label{Sec:T:Computers:Storing_Data_in_Memory}

It might seem that a lot of work will have to go into remembering what data is stored in what memory address.  Fortunately, this can be abstracted using by using \cite{variable}s, which are essentially named placeholders for the memory addresses.  Don't confuse variables in coding with variables in math, there are some important differences.  In coding, variables are bits of text that (hopefully) succinctly describes the nature of the information that is stored in a specific part of the memory.  

In an abstract sense, people think of variables as ``holding'' data for you, even if that is not really what is happening behind the scenes. A bit more accurately, a variable points to a location in \cite{memory} where data is stored.  A variable must always have some value, and as a programmer you can either use that value in a computation, create a new variable with some value, or replace the current value in a pre-existing variable with a new one (in most cases).  

The two main uses of variables are in \cite{expression}s, which are just mathematical instructions, and \cite{assignment} \cite{operation}s, which produce a result that you want the computer to store at least temporarily.  In Chapter \ref{Chap:N:00ExpDes}, you can see how to use the values in a variable, by writing a mathematical expression in which the variable's name is used rather than explicitly writing its value.  To create or change the value stored in a variable, you must use as assignment operation, in which the variable name into which you would like to store the result is always on the left hand side of the assignment operator (equals sign), and the expression you would like to evaluate is on the right hand side.  When executing this line of code, the computer will first evaluate the expression on the right hand side, and then store the resulting value in the memory, pointed to by the variable on the left hand side. Behind the scenes, what Python is doing is adding the variable name, and the place in memory that it points to, into the so-called `namespace'---the collection of \cite{object}s that Python has access to.

Different languages have different conventions, but in Python, variable names must start with a letter (A-Z or a-z) or an underscore (\_), but cannot begin with a number. They can contain letters, numbers, and underscores, but no other special characters or spaces. Variable names are case-sensitive (myVar and myvar are different), and they cannot be the same as Python's reserved keywords (e.g., \lstinline{round}, \lstinline{abs}, \lstinline{def}). For readability, it is recommended to use descriptive names and follow your favorite conventions for diambiguating between variable names, \cite{function} names, and \cite{class} names.



\subsubsection{Type of Data Stored in a Variable}

In some programming languages, you have to decide what type of data (i.e. text, numeric values, etc., more on that in Chapter \ref{DataTypes}) will be stored in the variable when you first create it, a concept called \textbf{static typing}.  However, in Python, the type of data that can be stored in a variable can change.  You can assign the number 1.507 to the variable \texttt{sample\_mass} and then later change that value to the text \texttt{`puppies`} and Python will not complain.  This can be both a good and a bad thing, but it is something you should be aware of. A complete discussion of these different \cite{data_type}s can be found in Chapter \ref{DataTypes}.



\subsection{How computers manipulate data}

Now that we've discussed some of the fundamentals how computers store data, we can talk in broad strokes about how computers perform computation on the data.  Computation in computers is the conversion of data based on instructions for how to convert the data.  Essentially every step of computation will create new data from existing data.  Computers have a limited subset of conversions that they can do, essentially arithmetic, moving data around, and some basic shortcuts that make common operations go faster.  Every computation thus involves many steps of these basic conversions, much like a complex chemical reaction involves many elementary reactions.

Data conversion in computers is performed by a ``controller'' or, in traditional computers, a ``microprocessor.''  We will leave a detailed explanation of how these components work to other resources, but give a brief overview here.  At its most basic, a processor is a series of logic gates that perform boolean operations on binary numbers.  These logic gates are made of transistors, and each operation that a processor can do is designed by hard wiring a series of these transistors together such that the result of the operation depends deterministically upon the combination of binary numbers being operated on.  For example, to add two numbers, the binary representation of them must first be loaded into specific registers, locations in the processor that store data only as long as necessary to complete an operation.  The operation is then triggered, and an output register is populated with a series of bits corresponding to the result of the operation.  The data in this output register must then be moved back to memory to allow the next operation to run.

Luckily, modern software developers do not have to think at this `low level' about how the computer works.  Extremely powerful programming languages, using systems much more abstract and easier for humans to think about, have been developed, and this is why it is possible for you to be an excellent chemist and a serviceable programmer simultaneously.

%\section{Parts of a Modern Computer}

%\subsection{Data Manifestation}

%\subsubsection{Inputs}

%\subsubsection{Outputs}

%\subsection{Storage}

%\subsection{Processing}

%\subsection{How do they all work together?}

\section{You are used to working with a ``software stack''}
\setcounter{subsection}{-1}  % Start subsections from 0

When you are doing work on a computer, you are typically thinking about the specific program or application that you are running.  These programs are very rarely self-contained, but rather depend on a number of other pieces of software, all of which work together to provide the functionality you need.

\subsection{The Operating System}

The operating system (OS) takes care of most of the housekeeping of your computer: talking to input and output devices (e.g., mouse and displays), managing files and folders, managing installed software, and a number of other important but often behind-the-scenes processes.  Common OSes you may encounter include Microsoft Windows, Apple MacOS (formerly OS X), various Linux distributions, iOS, and Android---though many others exist as well.  You can think of the OS as the ``host'' of everything that runs on your computer, and most software must be written somewhat differently for different specific OSes.  The ``widgets'' such as buttons, scroll bars, or menus that you are used to interacting with when using applications are one example of this OS-dependence, requiring user interfaces to be rewritten for different OSes.  Luckily, cross-platform (i.e., works the same across different OSes) user interface packages exist to make this easier, though they sacrifice the look of a ``native' application.

\subsection{Libraries}
\setcounter{subsubsection}{-1}  % Start subsections from 0

If you, as a programmer, had to figure out how to make a scroll bar, or access data on a website, yourself, it would take a long time to produce new software!  Luckily, nearly all of these problems have already been solved, and those solutions are often made available to you in a modular form known as \acite{libraries}.  Libraries allow you to borrow other programmers' solutions to common problems, so that you don't have to figure out the solution yourself.  This saves you time and makes it more likely that your code will have fewer bugs.  Python features an extensive set of standard libraries, and its users have created a huge array of other libraries that you can use with just a small amount of effort.  Many chemistry-specific libraries are available, such as RDKit (introduced in Chapter \ref{03DrawMol}).

Libraries (should) come with documentation (that can be of varying quality) that explain to you how to use the library without having to know the implementation details of the code behind the scenes.  Note that many of these libraries are free and open-source, which means that you are depending on volunteers to produce the code and documentation and to maintain them.  The pitfall here is that some libraries are often abandoned or have minimal technical support available, and it is up to you to try to evaluate the longevity of the library you are using and validate that they work as intended.

\subsubsection{Software Development Kits}

An important subset of libraries are Software Development Kits (SDKs), which (in the context of chemistry) are sometimes available as companions to applications that control instruments or perform specific chemistry tasks.  SDKs are libraries that allow you to control, or make use of capabilities of, applications for which you do not have source code.  This could be useful, for example, if you need to write a code that automates experiments involving several pieces of equipment that need to be synchronized in some way.

\subsection{Applications}
\setcounter{subsubsection}{-1}  % Start subsections from 0

Applications are the software that you mostly use on a day-to-day basis: web browsers, instrument control software, spreadsheets, and more.  These are the programs that you are likely most familiar with.  Applications come in many shapes and sizes, from complex programs like Microsoft Powerpoint to simple widgets like calculators.  Applications are, in some ways, self-contained.  They may require libraries on which they depend to be installed on your computer, but they typically do not require some other software other than the OS to be running in order to function.

\subsection{Scripts}
\setcounter{subsubsection}{-1}  % Start subsections from 0

Scripts are pieces of software that \textit{do} require some other software to be running in order to function, and thus are not self-contained in the way that applications are.  The programs produced in the narrative part of this book are scripts, just because it is easier to not have to worry about the coding overhead to make a self-contained application.  Scripts may impose a performance penalty over applications, but typically this performance penalty is negligible---especially for the workflows discussed in this book---and well worth the trade-off for ease of development.

\end{document}




% removed the data type dsiscussion from here


\subsubsection{Integer Numbers}

You are used to thinking of integers like 4 or 22 as the fundamental representation of countable quantities.  However, this is merely a representation of integers, one of many (like binary) that could be chosen essentially arbitrarily.  By and large, humans count and do math with an integer representation system called base-10 (probably because most of us have ten fingers and toes), where binary is more generally called base-2.  Base refers to the number of integers that we can represent with a single digit.  In base 10, this is 0 through 9 for a total of ten.  

Once we try to represent numbers beyond these single digits (for instance, the number ten), we no longer have a single symbol to represent integer, so instead we use a pair of symbols, 10, where it is understood that the first symbol tells us how many units of 10 make up the integer, and the second, how many units of 1.  In general, the number of integers we can represent with $N$ digits in base ten is $10^N$.

These same arguments holds for base-2 (binary) counting.  With four binary digits (four bits), we can represent $2^4$ or 16 integers. We would then represent the number thirteen as: \texttt{1101}.  Here, the first digit tells us how many units of eight we have, the second, how many units of four, the third, how many units of two, and the last, how many units of one.  Thus, we can think of this as $(1 \cdot 8) + (1 \cdot  4) + (0 \cdot 2) + (1 \cdot 1)$, which obviously yields thirteen. 

The above system is the basis for so-called ``unsigned integers," which can only be positive.  Such a system is called an encoding, and standard encodings have been created for a vast array of information that we might want to represent in a computer.  If we were to want to represent negative integers, it is our perogative to add an extra digit (typically at the beginning of the series of digits) to specify positive or negative.

\subsubsection{Alternate Representations of Binary Numbers}\label{Sec:T:Computers:BinaryToHex}

It is very difficult for humans to read, interpret, and remember long strings of digits, regardless of the base.  This is why phone numbers are often subdivided by ``()" or ``-" symbols.  The situation is most exacerbated for binary numbers, which require the most digits to represent a given quantity of information.  Imagine having to spend your days dealing with information that looks like \texttt{0110010100001010110010000110111101110010011100010110100011010111}.  This is a 64-bit number, the standard for computers at the time of this writing.  Clearly, finding a way to represent this number in a way that makes it easier for humans is desirable.  We could convert the number to base-ten, but this carries with it severe drawbacks.  Since ten is not a power of two, it is difficult to tell at a glance how many bits or bytes it might represent. 

The most common compromise between human readability and connection to binary is found in base 16, which allows 4 bits to be represented as a single digit using the following symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.  We now have sixteen digits, and so we call this representation \cite{hexadecimal}. Conversion from binary to hexadecimal is straightforward: we have four bits, and just like in base-10, the rightmost bit is (usually) the least significant one.  So, $0101 = 0 \times 8 + 1 \times 4 + 0 \times 2 + 1 \times 1 = 5$ and $1010 = 1 \times 8 + 0 \times 4 + 1 \times 2 + 0 \times 1 = 10$. In this notation, A is what we know as ten in base-10, and F is therefore 15.  In this representation, our 64-bit number that used 64 digits in base-2 requires only 16 digits, which takes up less room on a screen, is easier to write down, and MUCH easier to remember.  To make it clear what base we are using, such numbers  often carry a prefix ``x" when written (x = hex). Thus, our 64 bit number would be written as x650AC86F727168D7.   Other systems exist and use different prefixes (e.g., o = octal = base 8 = 3 bits), but are less frequently used.

\subsubsection{Text Characters}

Counting integers makes some intuitive sense in binary, but not all data can be represented in such a mathematically natural way.  In any case, it is up to us to determine these encodings.  As long as everyone agrees to use the same set of encodings, we can represent almost any concept we can think of.  As an example of a more arbitrary encoding, we turn to text characters, which could be letters, numerals (importantly, these are visual representations of numbers, not the numbers themselves---you can't do math with numerals!) special symbols such as ``\%," or even text concepts that are not directly visualizable, like moving to the beginning of a new line of text.  There are, in principle, an infinite number of encodings that could be created for text, but there are a few that have gained wide use.  One simple example is \textbf{ASCII}, which uses 7 bits and therefore can represent $2^7=128$ characters.  ASCII characters are often denoted by their corresponding unsigned integer, or by a two-digit hexadecimal number (e.g., ASCII code x5C yields a \textbackslash character).  As long as we all agree which ASCII characters go with which binary numbers, we can represent and transmit text!  You can read more on this in Chapter \ref{Text}.
