# SNC Introduction

The aim of this document is to introduce the concepts needed to describe a stochastic network and its dynamics, and to explain how such network can be controlled.

Note that the discussion related to how to optimally control a stochastic network is out of the scope of this document. This topic is discussed in details in the Hedgehog notebook.

## How to use this document?
This notebook is conceived as a counterpart to real-live tutorial at the whiteboard, which SNC members present to newcomers to the team. Thus most of the concepts and diagrams make most intuitive sense when presented/drawn live and their relations to other items can be reiterated verbally and graphicaly on the board.

The tutorial itself was meant to characterise the notion of Stochastic Network Control using very basic and intuitive mathematical concepts. Through this process very relevant insights relating to the network control were revealed.

## What network?

Any generic network has two constituitive elements: network nodes and the links/edges connecting individual nodes to each other as well as to the world ouside of network boundaries.

In SNC network nodes are represented as a collection of buffers where the queues of items accumulate with time. Items can be arbitrary jobs, packets, clients, stock inventory, etc. The current number of items in all the buffers define the state of the network.

The flow of items into/out-of/within the network boundaries is determined by set of activities which essentially take the role of directed links/edges in the network. Each activity "transfers" items from one set of buffers to another and can stand for

- manufacturing process - where items from different input (with respect to activity) buffers are combined/transformed to create different items in output buffers

- transportation process - where items preserve their identity but move to another location such as between warehouse and retail inventories

- servicing process - where clients (in a bank, hospital, airport) are serviced and are directed to the next stage
- etc.

Activities are naturally performed "in time" and therefore have characteristic processing rates, literally meaning how many jobs/items can be processed/transfered between the input and output buffers per unit of time.

In accordance with real life systems there are associated costs with having items waiting in the buffer queues and with performing activities for decided periods of time.

In addition to buffers(nodes) and activities(directed links) there is a third type of elements characteristic to SNC networks - resources. Resource is an entity (physical typicaly) which performs a selected set of activities. Drawing from the examples for items and activities mentioned above, corresponding resources will be processing equipment, transportation vehicles and service personnel. The underlying physical constraint on individual resources is that they cannot perform multiple activities simultaneously.

Resources are the only controllable elements within the network and the associated decision variables are:
- when to engage the resources on selected activites _and_
- for how long should they operate _and_
- in what sequence relative to each other

Typically items can be independently entering (or leaving) the network boundaries at predetermined locations (buffers). These activities are external and uncontrollable by the resources of the network in question.

## What control?

Using the three fundamental building blocks from above we can formulate a provisional definition of a network control: by managing the working time of resources on selected activites we can change the state of the network (i.e. the number of items in each of its buffers).

So far this provisional definition has an element of arbitrariness to it since we are not specifying why we want this or that particular change in the state of the network. It is equivalent to specifying how steering and pushing pedals changes the speed and position of the car without even considering that we ultimately want to use the car as means of transportation from one location to another.

The more criterion based notion of network control requires introduction of some sort of end-goal-state or end-goal-sequence-of-states of the network. For the car analogy this would be to specify further that basic controls need to be used to maintain the car on the road at steady speed. For the networks we are considering the end-goal-state might be the one where all the buffers are empty (no more customers waiting in the queues) or at some provided set-point level (such as inventory safety stock level).

Now the requirement of optimality in controlling the network specifies _how_ the end-goal-state of the network has to be reached. Possible optimality criteria for deciding between alternative action sequences to reach the end-state include minimum time to reach desired end-goal-state of the network, minimum cumulative activity cost incured, minimum cumulative network state cost incured or both. Mentioning the driving analogy for the last time, optimal objective can be to reach end destination in the shortest amount of time and/or using the least amount of fuel.

## What stochasticity?

To conclude this section we need to clarify in what sense we use term "stochastic" when speaking of networks and their control. The more limited use of "stochastic" in our context refers to the fact that when activities are performed the amount of items being processed at each timestep might not be at the exact deterministic rate, but rather on average at this "mean rate" plus-or-minus some noisy variation. This use will be assumed throughout this notebook and most of the more advanced tutorials to follow.

The broader use of "stochastic" as applied to networks of interest refers to unpredictable shock events such as failures of resources, loss of items in the buffer queues or excessive amount of new item arrivals into the network. We do not seem to be modelling this events explicitly but rather expect the optimally derived policy (Hedgehog) to adapt instanteneously to the sudden change in the state of the network. These "unpredicatbility" is beyond the scope of this introductory tutorial.

## Notation

#### The notation is NOT fully consistent with the Hedgehog notebook

$n_b$: number of buffers

$n_r$; number of resources

$n_a$: number of activities

State vector
$ \begin{align}
    \underline{q}=
          \begin{bmatrix}
           q_1\\
           \vdots \\
           q_{n_b}
          \end{bmatrix}
  \end{align}$,
where $q_i$ identifies how many items are in buffer $i$.

Activity time vector
$ \begin{align}
    \underline{z}=
          \begin{bmatrix}
           z_1\\
           \vdots \\
           z_{n_a}
          \end{bmatrix}
  \end{align}$,
where $z_i$ identifies how much time activity $i$ is active.

Buffer processing matrix
$ \begin{align}
    B =
          \begin{bmatrix}
           \mu_{1, 1} & \dots & \mu_{1,n_a}\\
           \vdots & \vdots & \vdots \\
           \mu_{n_b, 1} & \dots & \mu_{n_b,n_a}
          \end{bmatrix}
  \end{align}$,
where $\mu_{i,j}$ identifies activity $j$ processing time of items in buffer $i$. In SNC we are going to consider a stochastic process for each resource and assume that $\mu_{i,j}$ is sample from a distribution whose mean is $\mu_{i,j}$.

Constituency matrix
$ \begin{align}
    C =
          \begin{bmatrix}
           c_{1, 1} & \dots & c_{1,n_a}\\
           \vdots & \vdots & \vdots \\
           c_{n_r, 1} & \dots & c_{n_r,n_a}
          \end{bmatrix}
  \end{align}$,
where $c_{i,j} = 1$ if activity $j$ can be performed by resource $i$ and $c_{i,j} = 0$ otherwise.

Resources time vector
$ \begin{align}
    \underline{k} =
          \begin{bmatrix}
           k_1\\
           \vdots \\
           k_{n_r}
          \end{bmatrix}
          = C \underline{z}
  \end{align}$,
where $k_i$ corresponds to the sum of the time of the activity the resource can perform. Note that $\underline{k}$ does not contain information about how the workload is split into activities and the activities sequence.

Worktime
$ \begin{align}
    \underline{w} =
          \begin{bmatrix}
           w_1\\
           \vdots \\
           w_{n_r}
          \end{bmatrix}
  \end{align}$,
where $w_i$ is the time resource $i$ has to work in order to drain the network.

Demand
$ \begin{align}
    \underline{\alpha}=
          \begin{bmatrix}
           \alpha_1\\
           \vdots \\
           \alpha_{n_b}
          \end{bmatrix}
  \end{align}$,
where $\alpha_i$ identify the number of items that are exogenously added to buffer $i$.

$\Delta \underline{x}$ is the variation over time $T$ of $\underline{x}$.

## Goal
### The goal of SNC is to regulate a stochastic network by controlling how much time each resource has to work, i.e., worktime $\underline{w}$, and the activity time $\underline{z}$.

## Single Activity Assumption

### We assume that each resource has only one possible activity, i.e., $C = I$

## Single Resource Assumption
Each activity can be performed only by a single resource. This is ALWAYS the case and thus the sum of the elements in each columns of the constituency matrix is at most 1.

## Push and Pull models

In this section, we describe two simple examples to illustrate one of the main differences networks can have.

### Push Model

A simple Push model describes a network in which an item can be processed only if it first input into the network as demand, i.e., the network cannot control the start of the process.  Given this, we can say that the demand is pushing items in the network, and thus refer to this model as Push model. An hospital processing patients arriving at the A&E is an example of such network model. Note that the arrival of patients cannot be controlled by the hospital.

$ \begin{align}
    \underline{q}=
          \begin{bmatrix}
           q_1\\
           q_2 \\
           q_3
          \end{bmatrix}
  \end{align}$,

$ \begin{align}
    \underline{z}=
          \begin{bmatrix}
           z_1\\
           z_2 \\
           z_3
          \end{bmatrix}
  \end{align}$

$ \begin{align}
    B =
          \begin{bmatrix}
           - \mu_s & 0 & 0\\
           + \mu_s & - \mu_p & 0 \\
           0 & + \mu_p & - \mu_d
          \end{bmatrix}|
  \end{align}$

### Pull Model (or Demand Driven Model)

The Pull model or Demand Driven model describes the case in which an item has to be process by multiple resources in order to be ready to be sold, i.e., matched with demand. In order to satisfy a new customer, i.e., demand, the item have already been processed by the chain of resources. Given this interpretation, we can say that the demand is pulling items through the network, and thus refer to this model as Pull model. An example of Pull model is the multi echelon inventory.

$ \begin{align}
    \underline{q}=
          \begin{bmatrix}
           q_1\\
           q_2 \\
           q_3
          \end{bmatrix}
  \end{align}$,

$ \begin{align}
    \underline{z}=
          \begin{bmatrix}
           z_1\\
           z_2 \\
           z_3
          \end{bmatrix}
  \end{align}$

$ \begin{align}
    B =
          \begin{bmatrix}
           + \mu_s & - \mu_p & 0\\
           0 & + \mu_p & - \mu_d \\
           0 & 0 & - \mu_d
          \end{bmatrix}
  \end{align}$

## Forward and Inverse problems

As mentioned at the beginning of this document, we are concerned with controlling the network by deciding to worktime of resources. In this section we formalise this problem, called Inverse problem. However, for the sake of explanation and to provide useful insights into the problem, we first introduce the Forward problem, in which the worktime is given and the unknown is the buffers state.

From the fluid model we know that:

$$ \underline{q_1} = \underline{q_0} + B \underline{z_1} $$

### Forward problem

The aim of the forward problem is to compute $\Delta \underline{q} = \underline{q_1} - \underline{q_0} $ given that $\underline{z}$ is known. From the fluid model we know that $ \underline{q_1} = \underline{q_0} + B \underline{z_1} $, and thus the Froward problem can be address with the following steps:

1) Compute $B$

2) Solve $ \Delta \underline{q} = B \underline{z} $

### Inverse problem

The inverse problem aim to compute $\underline{z}$ and $\underline{k}$ given that $\Delta \underline{q}$ is known.
Thus, we can address the problem by following the steps described below:

$$ \Delta \underline{q} \xrightarrow{(1)} \underline{z} \xrightarrow{(2)}\underline{k} $$

$$ \text{(1)   } \underline{z} = F \Delta {\underline{q}}$$

$$ \text{(2)   } \underline{k} = C \underline{z} = CF \Delta \underline{q} $$

where $F$ is such that $FB = I$.

If B is a square matrix, then $F = B^{-1}$ and thus $ \underline{z} = B^{-1} \Delta \underline{q} $ and $\underline{k} = CB^{-1} \Delta \underline{q} $.

Note that, differently from the Forward problem, the inverse problem require to invert B. This may not be possible of computationally expensive.

### Problems with Demand

We can include demands over a time period $T$ as follows:
$$ \Delta \underline{q} = B \underline{z} + \underline{\alpha} T $$

## Draining the Network: a special case of the inverse problem

This section considers a special case of the inverse problem where the goal is to drain the network, i.e., $ \Delta \underline{q} = \underline{0} - \underline{q} = - \underline{q}$. Thus, we can make the following substitution:

$$ \underline{k} = CB^{-1} \Delta \underline{q} $$

$$ \underline{k} = CB^{-1} (- \underline{q}) $$

$$ \underline{k} = C(-B^{-1}) \underline{q} $$

Following Sean's notation, we denote the worktime required to drain the network shown above (in this specific case we are considering no new arrivals, i.e., $ \underline{\alpha} = \underline{0} $), with $ \underline{w} $.

$$ \underline{w} = C(-B^{-1}) \underline{q} $$

Moreover, let $ \Xi = C(-B^{-1}) $ and thus

$$ \underline{w} = \Xi \underline{q} $$

## Rate of Change

In SNC we are going to work with rate of change of buffers, activities times, and work time. Thus, we are going to differentiate w.r.t time the equation of the system and obtain:

$$ \underline{\dot{q}}  = B \underline{\zeta} + \underline{\alpha} $$

Note that if the resources idle, i.e., $\underline{\zeta} = \underline{0}$, then the rate of growth of the buffer is equal to the demand, i.e., $ \underline{\dot{q}}  = \underline{\alpha} $. Thus, in order to process the demand or drain the network $ \underline{\dot{q}} < 0 $.

Similarly to what explained in the previous section, the steps needed to solve the inverse problem are the following

$$ v = \underline{\dot{q}} \xrightarrow{(1)} \zeta \xrightarrow{(2)} C\zeta $$

and $\underline{\dot{q}}$ is also referred to as velocity and denoted by $v$.

#### Resource time rate $C\underline{\zeta}$  and Worktime rate $\underline{\dot{w}}$.
It is important to remark the difference between the resource time rate, $C\underline{\zeta}$, and the rate of the work that (still) needs to be done in order to drain the network, $\underline{\dot{w}}$. In particular,

$$ C \underline{\zeta} \text{ positively correlated with } \underline{\zeta} $$

$$ \underline{\dot{w}} \text{ negatively correlated with } \underline{\zeta} $$

$$ \underline{\dot{w}} \text{ positively correlated with } \underline{\rho} $$

where $ \underline{\rho} $ is the load balancing vector and is discussed in the next section.

## Load Balancing, a special case of Rate of Change

In this section, we consider the case where the aim is to drain only the demand $\underline{\alpha}$ and not the overall network, i.e., $\delta \underline{q} = \underline{0}$ and $\underline{\dot{q}} = 0$, and we are answering the question 'at which rate the resources have to work in order to drain the demand?'.

Thus, given $\underline{\dot{q}} = B\underline{\zeta} + \underline{\alpha}$ the aim is $-\underline{\alpha} = B\underline{\zeta}$.

The load balancing vector, denoted by $\underline{\rho}$, corresponds to the $\underline{\zeta}$ such that $ - \underline{\alpha} = B\underline{\zeta}$, i.e. $ \underline{\rho} = -B^{-1}\underline{\alpha}$

Note that, since we consider $C = I$, then $-B^{-1} = C(-B^{-1})$ and this we can write the previous equation as:

$$ \underline{\rho} = \Xi \underline{\alpha}$$

Note that $\underline{\rho}$ represents the rate at which the demand is drained and is constant because $\Xi$ represent the property of the network (that does not change) and $\alpha$ is exogenously given.

## $\underline{\rho}$ and the Workload

We know that $ \underline{\dot{q}} = B \underline{\zeta} + \underline{\alpha}$ and that we can safely multiply everything by $\Xi$.

Thus, by observing that $\Xi B = C(-B^-1)B = -CI = -C$ and that $\Xi \underline{\alpha} = \underline{\rho}$, we can state that

$$ \Xi \underline{\dot{q}} =  \Xi B \underline{\zeta} + \Xi \underline{\alpha}$$

is equivalent to

$$ \Xi \underline{\dot{q}} =  -C \underline{\zeta} + \underline{\rho}$$

Given that $ -C \underline{\zeta}$ defines how much the rate of the worktime to drain the network, i.e.,  ($\underline{\dot{w}}$) decreases due to the activity rate and that $\underline{\rho}$ instead defines how much it increases due to the arrive rate of new demand, we know that

$$ \underline{\dot{w}} =  -C \underline{\zeta} + \underline{\rho}$$

$$ \underline{\dot{w}} = \Xi \underline{\dot{q}} $$

From this analysis we can derive 3 important observations:

#### Observation 1: all resources idle

If $\zeta = 0$, then $\underline{\dot{w}} = \underline{\rho}$.

This implies that the work to drain increases of $\underline{\rho}$ every time the resources do not work, i.e.,
$$ \underline{{w}_1} \rightarrow \underline{{w}_0} + \underline{\rho} \Delta T$$

#### Observation 2: load balance

If $C\zeta = \underline{\rho}$, then $\underline{\dot{w}} = 0$.

This implies that if the work done by the resources just satisfy the new demand (i.e., load balance), then work needed to drain the network never decreases and the network will never be drained.

#### Observation 3: resource operating 100%

If $C\zeta = 1$, then $\underline{\dot{w}} = - (1 - \underline{\rho})$.

This implies that if the resources are working at full capacity, then the work to drain decreases of$ (1- \underline{\rho}) \Delta T $, i.e.,
$$ \underline{{w}_1} \rightarrow \underline{{w}_0} - (1 - \underline{\rho})\Delta T $$

### Time to drain the network
Given that $\underline{\rho}$ is the load balancing vector and the $C\zeta = 1$ when the resources operated at full power, we know that $1 - \underline{\rho}$ is the time the resources can work to empty what is already in the buffer (not taking into account the new arrival).

Call $\underline{w}^*$ the time needed to drain the network when there are no new arrivals. Now we are ready to compute the time to drain the network when resources operate at full power as:

$$ T^* = \max_{i \in resources} \frac{w^*_i}{(1 - \rho_i)} $$

## How to obtain $B^{-1}$

In this section, we look at how to obtain information about $B^{-1}$ without explicitly computing it and we introduce the concept of message passing related to SNC.

We remark that we are focusing on the inverse problem, and thus the unknown is $\underline{z}$.

### Push model case

For the Push model, $ B \underline{z} = \Delta \underline{q} $ corresponds to

$
          \begin{bmatrix}
           - \mu_s & 0 & 0\\
           + \mu_s & - \mu_p & 0 \\
           0 & + \mu_p & - \mu_d
          \end{bmatrix}
          \begin{bmatrix}
           z_s\\
           z_p \\
           z_d
          \end{bmatrix}
          =
          \begin{bmatrix}
           \Delta q_1\\
           \Delta q_2 \\
           \Delta q_3
          \end{bmatrix}
$

We can exploit the fact that the matrix is triangular and compute all the elements of $\underline{z}$ with the following sequential steps:

Step 1: $z_s = \frac{-\Delta q_1}{\mu_s}$

Step 2: $z_p = \frac{+ \mu_s z_s - \Delta q_2}{\mu_p} = \frac{-\Delta q_1 - \Delta q_2}{\mu_p}$

Step 3: $z_d = \frac{+ \mu_p z_p - \Delta q_3}{\mu_d} = \frac{-\Delta q_1 - \Delta q_2 - \Delta q_3}{\mu_d}$

From this, we can obtain 'for free' the matrix $B^{-1}$:

$B^{-1} =
\begin{bmatrix}
           - \frac{1}{\mu_s} & 0 & 0\\
           - \frac{1}{\mu_p} & - \frac{1}{\mu_p} & 0 \\
           - \frac{1}{\mu_d} & - \frac{1}{\mu_d} & - \frac{1}{\mu_d}
          \end{bmatrix}
$

### Pull model case

For the Pull model, $ B \underline{z} = \Delta \underline{q} $ corresponds to

$
          \begin{bmatrix}
           + \mu_s & - \mu_p & 0\\
           0 & + \mu_p & - \mu_d \\
           0 & 0 & - \mu_d
          \end{bmatrix}
          \begin{bmatrix}
           z_s\\
           z_p \\
           z_d
          \end{bmatrix}
          =
          \begin{bmatrix}
           \Delta q_1\\
           \Delta q_2 \\
           \Delta q_3
          \end{bmatrix}
$

We can exploit the fact that the matrix is triangular and compute all the elements of $\Delta \underline{k}$ with the following sequential steps:

Step 1: $ z_d = \frac{-\Delta q_3}{\mu_d}$

Step 2: $ z_p = \frac{+ \Delta q_2 + \mu_d z_d }{\mu_p} = \frac{+ \Delta q_2 -\Delta q_3 }{\mu_p}$

Step 3: $ z_s = \frac{+ \Delta q_1 + \mu_p z_p }{\mu_s} = \frac{+ \Delta q_1 + \Delta q_2 -\Delta q_3 }{\mu_s}$

From this, we can obtain 'for free' the matrix $B^{-1}$:

$B^{-1} =
\begin{bmatrix}
           +\frac{1}{\mu_s} & + \frac{1}{\mu_s} & - \frac{1}{\mu_s}\\
           0 & + \frac{1}{\mu_p} & - \frac{1}{\mu_p} \\
           0 & 0 & - \frac{1}{\mu_d}
          \end{bmatrix}
$

### Message Passing

Note that, generally, two forms of control can be applied to a buffer: control over the input and control over the output.

By observing the matrix $B^{-1}$ in the two cases above, we can infer that there is a sort of message passing among resources that propagate from resources that are connected to buffers where only one for of control is available to the others.

Let's consider the Push and Pull models.

In the Push model, by observing the first row of $B^{-1}$, we can infer that work done by resource $r_1$ depends only on the requirement over the first buffer, i.e., it depends on $q_1$, (begin the last two elements of the row equal to zero). The message is then passed from $r_1$ to $r_2$. Thus, the work that $r_2$ has to do depends on $q_1$ and $q_2$. Then, $r_2$ propagate the message to $r_3$ that then has to take into account the requirements for all the buffers. The direction of the message passing in the Push model is shown by the gray lines in the image below. The black double arrow indicate the constraints that the buffer with a single way of control impose on the connected resource, we also say that a resource/activity is responsible for the buffer it is connected with the black arrow.

In the Pull model, by observing the third row of $B^{-1}$, we can infer that work done by resource $r_3$ depends only on the requirement over the buffer 3, i.e., it depends on $q_3$ (begin the first two elements of the row equal to zero). The message is then passed from $r_3$ to $r_2$. Thus, the work that $r_2$ has to do depends on the requirements over both buffers $q_2$ and $q_3$. Finally, $r_2$ propagate the message to $r_1$ that then has to take into account the requirements for all the buffers. The direction of the message passing in the Pull model is shown by the gray lines in the image below.

The message passing can be interpreted as follows: resource $r_i$ that is connected to the most constrained buffer $q_i$, i.e., the one with only one way of control (one input or one output), has to work to satisfy the requirement over such buffer, i.e., $\Delta q_i$. By taking this decision, $r_i$ may have also implicitly defined the work it is going to do on another buffer $q_j$, and thus created the situation where $q_j$ can now be controlled in only one way. Thus, $r_i$ propagated its decision, i.e., $w_i$, to the resource $r_j$ directly connected to buffer $q_j$. Upon receiving such information, $r_j$ decides how much time to work in order to satisfy the requirement over $q_j$ given the work $r_i$ has already decided to do. This is then repeated in a dynamic programming fashion until no more buffers can be controlled by resources only in one way. The following image illustrate these steps.

Given the process described above, no more buffers can be controlled only in one way in the following 3 cases:

1) the message flow has been successfully computed

2) the message flow cannot be fully computed

3) the message flow is not unique

In the following sections we show examples of case 1 and 2.

#### Other feasible examples:

Example 1: there is an extra demand $\alpha''$ as input to buffer 2. However, the buffer still has two ways of control and thus the flow is the same as the basic pull model.

Example 2: in this case the network has two buffers with only one way to be controlled. The message propagation starts from both of them, meets at buffer 2 ('buffer meeting point'), and then is propagated to resource 2, the only resource that can still control the buffer.

#### Unfeasible examples:

Example 1: As in the example before, here there are two buffers with only one possible control. Both the buffers are imposing constraints to resource $r_3$. In particular, $\Delta q_3 = \mu_3 z_3$ and $\Delta q_3 = \mu_3 z_3$. Thus, unless $\Delta q_3 = \Delta q_4$, the network has no solution.

Example 2: As in Example 2, here there are two buffers with only one possible control and both start propagating their decision. However, there is no 'buffer meeting points' (like buffer 2 in Example 2) that, after receiving the two messages, still has (at least) a way to be controlled.
Alternatively, as explained in the example above, we can say that in general there is no resource that can satisfied the constraints imposed by the received messages (exception is done for some specific initial and desired buffer states).
Thus, generally, this network does not admit a solution.

### Lesson learned 1: the Zeros

Consider an element $b^{-1}_{i,j}$ of matrix $B^{-1}$ where $i$ refers to an activity (or resource, given the single activity assumption) and $j$ refers to a buffer.

We note that, all $b^{-1}_{i,j}$ such that buffer $j$ follows activity/resource $i$ given the message flow are equal to 0.

For example, in the Pull model,
buffer 1 and 2 follow $r_3$, buffer 1 follows $r_2$, and no buffer follows $r_1$ given the message flow. This implies that the zero in the $B^{-1}$ are as follows.

$B^{-1} =
\begin{bmatrix}
           \cdot & \cdot &  \cdot\\
           0 & \cdot & \cdot \\
           0 & 0 & \cdot
          \end{bmatrix}
$

### Lesson learned 2: the Correlations

The sign of the non zero elements in $b^{-1}_{i,j}$ depends on how the activity time $i$, i.e., $z_i$, correlates with buffer $j$.

In order to do this we can think at the work of activity $i$ as the linear sum of the following components:

1) $z_i^*$: the work that activity $i$ has to do to satisfy the requirements to the buffers it is responsible for, given the message flow, assuming that all the other activities are not working, i.e., as if activity $i$ and the buffer are in isolation

2) $z_i^h$: the work that activity $i$ has to do to compensate for the indirect effect that activity $h$ has over buffer $j$ given that the goal is to maintain the same number of items in buffer $j$ (buffer $j$ is the buffer resource $i$ is responsible for). Note that there is one $z_i^h$ for every resource $h$ that precedes resource $i$ given the message passing flow.

The correlation between $z_i^*$ and $\Delta q_j$, where $j$ is the buffer activity $i$ is responsible for, defines the sign of $b^{-1}_{i,j}$.

The correlation between $z_i^h$ and $z_h$ defines the sign of $b^{-1}_{i,j}$, where $i$ is the activity and $j$ is the buffer activity $h$ is responsible for.

Let's consider the Pull model:

##### $ z_d $ correlations

$$ z_d = z_d^* $$
where $ z_d^* $ refers to the work $r_3$ as to do to guarantees that $\Delta q_3$ is satisfied.

We observe that $ z_d^* $ is negatively correlated with $\Delta q_3$ because a positive work of resource $r_3$ corresponds to a decrease of number of items in buffer 3. In general, a resource is positively correlated with the buffer it is responsible for if it controls the input to such buffer, if instead it controls the output of the buffer, then the correlation is negative.

##### $ z_p $ correlations

$$ z_p = z_p^* + z_p^d $$
where $ z_p^* $ refers to the  work $r_2$ as to do to guarantees that $\Delta q_2$ is satisfied when no other resource is working and $z_p^d$ is the work $r_2$ has to do to guarantee that $\Delta q_2$ is satisfied when also (and only) $r_3$ is working.

We observe that $ z_p^* $ is positively correlated with $\Delta q_2$, and that $z_p^d $ is positively correlated with $z_d$ and thus negatively correlated with $\Delta q_3$. $z_p^d $ is positively correlated with $z_d$ because when $r_3$ is working positively, it is taking items out of buffer 2, and thus, $r_2$ has to positively work too to add items to buffer 2 in order to keep the stock invariant.

##### $ z_s $ correlations

$$ z_s = z_s^* + z_s^p + z_s^d $$
where $ z_s^* $ refers to the  work $r_2$ as to do to guarantees that $\Delta q_1$ is satisfied when no other resource is working, $z_s^p$ is the work $r_1$ has to do to guarantee that $\Delta q_1$ is satisfied when also (and only) $r_2$ is working, and $z_s^d$ is the work $r_1$ has to do to guarantee that $\Delta q_1$ is satisfied when also (and only) $r_3$ is working.

We observe that $ z_s^* $ is positively correlated with $\Delta q_1$, that $z_s^p $ is positively correlated with $z_p$ and thus positively correlated with $\Delta q_2$, and that $z_s^d $ is positively correlated with $z_d$ and thus negatively correlated with $\Delta q_3$.

##### The sign to $B^{-1}$ are:

$B^{-1} =
\begin{bmatrix}
           + & + & - \\
           0 & + & - \\
           0 & 0 & -
          \end{bmatrix}
$

#### Elements of $\underline{z}$ are not always positively correlated

In the figure below, an example in which not all elements of $\underline{z}$ are positively correlated. In Particular, $z_1^2$ is negatively correlated to $ z_2$ because if $r_2$ is working positively and thus adds items to buffer 2, then $r_1$ has to work negatively to remove such item in order to maintain the stock level invariant.

### Lesson learned 3: the Coefficients

Here we focus on how to obtain the coefficients of the non zero elements in $B^{_1}$.

Given that the signs have been considered in the section before, here we do NOT consider those at all. We assume that everything is positively correlated and then adjust the sign as prescribed in the previous step.

As example, let's consider the Pull model.

In order to compute the non zero coefficients of $B^{_1}$, we need to consider the time an activity $i$ needs to work in order to satisfy the requirement (desired state) of the buffer ($j$) it is responsible for, i.e., $z_i^*$, and the time $i$ it has to work to compensate for the other activity (or activities) if any that directly affect buffer $j$, i.e. $z_i^h$ where $h$ is the activity (or activities) that immediately precedes buffer $j$ given the message flow. It is important to remark that not all the $z_i^h$ used in the decomposition explained in the previous section are needed to compute the coefficients.

REMEMBER: we do not take into account correlations and thus we completely disregard the signs!!!

#### Coefficients of $B^{-1}$ related to $r_3$

$$ z_d = z_d^* = \frac{\Delta q_3}{\mu_d}$$
because $\Delta q_3 = \mu_d z_d^*$

#### Coefficients of $B^{-1}$ related to $r_2$

$$ z_p = (z_p^*,  z_p^d) = (\frac{\Delta q_2}{\mu_p}, \frac{\Delta q_3}{\mu_p}) $$
because:

1) $z_p^*$ is computed as above and thus $z_p^* = \frac{\Delta q_2}{\mu_p}$.

2) since $z_p^d$ aims to counterbalance and nullify the effect of the work done by resource $r_3$, and given that the two resources work at different speeds, in order to compute $z_p^d$ we need to scale the work of $r_3$ by their relative rates $\frac{\mu_d}{\mu_p}$. Thus we obtain $z_p^d = \frac{\mu_d}{\mu_p} z_d^* = \frac{\mu_d}{\mu_p}\frac{\Delta q_3}{\mu_d} = \frac{\Delta q_3}{\mu_p} $

#### Coefficients of $B^{-1}$ related to $r_1$

$$ z_s = (z_s^*, z_s^p)  = (\frac{\Delta q_1}{\mu_s}, \frac{\Delta q_2}{\mu_s}, \frac{\Delta q_3}{\mu_s}) $$
because:

1) $z_s^* = \frac{\Delta q_1}{\mu_s}$

2) $z_s^p = \frac{\mu_p}{\mu_s} z_p = (\frac{\mu_p}{\mu_s}z_p^*,  \frac{\mu_p}{\mu_s} z_p^d) = (\frac{\mu_p}{\mu_s}z_p^*,  \frac{\mu_p}{\mu_s} z_d) = (\frac{\mu_p}{\mu_s}z_p^*,  \frac{\mu_p}{\mu_s} z_d^*) = (\frac{\mu_p}{\mu_s}\frac{\Delta q_2}{\mu_p}, \frac{\mu_p}{\mu_s}\frac{\Delta q_3}{\mu_d}) = (\frac{\Delta q_2}{\mu_s}, \frac{\Delta q_3}{\mu_s})$

Note that, since the computation done in one step is used in the following steps, the only activity times that needs to be computed are the $z_i^*$ for every resource $i$.

#### The final $B^{-1}$

From the above equations, we derive the coefficient of the non zero elements in $B^{-1}$ that finally looks like:

$B^{-1} =
\begin{bmatrix}
           +\frac{1}{\mu_s} & + \frac{1}{\mu_s} & - \frac{1}{\mu_s}\\
           0 & + \frac{1}{\mu_p} & - \frac{1}{\mu_p} \\
           0 & 0 & - \frac{1}{\mu_d}
          \end{bmatrix}
$

#### Observations 1:

It is important to observe that each activity $i$ has to compute only $z_i^*$ to obtain both the sign and coefficient  for the buffer it is responsible for, i.e., buffer $j$. For the other buffers, resource $i$ just need to identify the correlation with the activity $h$ that immediately precedes $j$ in the message flow, and then 'inherits' from $h$ both the sign and the coefficient. In particular, if the correlation between $i$ and $h$ is positive, the missing elements in the row of $B^{-1}$ related to $i$ are exactly the corresponding elements in the row related to $h$; otherwise, if the correlation is negative, $i$ would 'inherit' $h$'s coefficients but with the opposite sign.

#### Observation 2:

It is possible to decompose the matrix $B^{-1}$ into two: one that takes into account the speed at which a resource process the items in the buffers (time workload matrix) and another that takes into account the amount of inventory that needs to be processes and how (item workload matrix).

$
           B^{-1} =
          \begin{bmatrix}
           \frac{1}{\mu_s} & 0 & 0\\
           0 & \frac{1}{\mu_p} & 0 \\
           0 & 0 & \frac{1}{\mu_d}
          \end{bmatrix}
          \begin{bmatrix}
           +1 & + 1 & - 1\\
           0 & + 1 & - 1 \\
           0 & 0 & - 1
          \end{bmatrix}
$

#### Example with elements of $\underline{z}$ not always positively correlated

STEP 1: identify the message passing flow

STEP 2: identify the zeros

$
          \begin{bmatrix}
           \cdot & \cdot & \cdot\\
           \cdot & 0 & 0 \\
           0 & 0 & \cdot
          \end{bmatrix}
$

STEP 3: identify the signs

$
          \begin{bmatrix}
           + & + & -\\
           - & 0 & 0 \\
           0 & 0 & -
          \end{bmatrix}
$

STEP 4: compute the coefficients (and obtain $B^{-1}$)

$
          B^{-1} =
          \begin{bmatrix}
           + \frac{1}{\mu_1} & + \frac{1}{\mu_1} & - \frac{1}{\mu_1}\\
           - \frac{1}{\mu_2} & 0 & 0 \\
           0 & 0 & - \frac{1}{\mu_3}
          \end{bmatrix}
$

Note that:

$
          B^{-1} =
          \begin{bmatrix}
           \frac{1}{\mu_1} & 0 & 0\\
           0 & \frac{1}{\mu_2}  & 0 \\
           0 & 0 & \frac{1}{\mu_3}
          \end{bmatrix}
          \begin{bmatrix}
           + 1 & + 1 & - 1\\
           - 1 & 0 & 0 \\
           0 & 0 & - 1
          \end{bmatrix}=
          \begin{bmatrix}
           + \frac{1}{\mu_1} & + \frac{1}{\mu_1} & - \frac{1}{\mu_1}\\
           - \frac{1}{\mu_2} & 0 & 0 \\
           0 & 0 & - \frac{1}{\mu_3}
          \end{bmatrix}
$

## $C \neq I$, what can we reuse

In this section, we show an example of how a network that does not have $C = I$ can be reconnected a network that has $C = I$ and, thus, how the $B^{-1}$ matrix of the latter network can be used to address the former one.

We consider the example in the image below.
The first network is a Pull model similar to the one discussed above whose $C = I$; while the second network has a resource that can perform multiple activities and thus the constituency matrix is
$C =
    \begin{bmatrix}
        1 & 0 & 0 & 0 & 0  \\
        0 & 1 & 0 & 1 & 0  \\
        0 & 0 & 1 & 0 & 0  \\
        0 & 0 & 0 & 0 & 1  \\
    \end{bmatrix}
$.

As shown in the image, the only difference between the two networks is that resource $r_2$ of the second network merges resources $r_2$ and $r_4$ of the first network.

Given this observation, we can compute the $B^{-1}$ matrix of the Pull network and then derive information about the second network by merging the second row and the forth row of $B^{-1}$.

#### Remark
It is important to remark that the activities do not merge but only the resources. This is why the equivalence hold.


# メモ

チャート式数学を Colab に写経するプロジェクト。

これは数学 Ⅰ。青チャート。 第1章 方程式と不等式

# 式の計算

**多項式 polynomial**

1. 数や文字だけの積の形を単項式と言う。単項式の和として表される式を多項式と言う。

1. 文字の部分が同じな項を同類項と言う。

1. 単項式の数の部分を係数、文字の指数の和を次数といい、次数が $n$ のとき $n$ 次式と言う。次数が 0 の項を定数項と言う。

**多項式の加法・減法と乗法**

1. 多項式の計算における基本法則
  - 交換法則 $\quad A+B=B+A,\quad AB=BA$
  - 結合法則 $\quad (A+B) + C = A+(B+C), \quad (AB)C=A(BC)$
  - 分配法則 $\quad A(B+C)=AB+ AC,\quad (A+B)C=AC+BC$

cf. commutative law, associative law, distributive law

1. 指数法則

  - $a^ma^n= a^{m+n}$
  - $(a^m)^n = a^{mn}$
  - $(ab)^n = a^n b^n$

多項式でないもの

$\quad \quad \displaystyle \frac{1}{x+1},~x+\frac{3}{x},~\sqrt{x^2+1},~x^2-2\sqrt{x}-\sqrt5$

多項式の整理 $\quad$ 項の次数の高いものから順に並べる $\cdots\cdots$ 降冪 (こうべき)

**基本例題 1**

次の多項式の同類項をまとめて整理する。 また 2, 3 の多項式において、[  ] 内の文字に着目したとき、その次数と定数項はなにか。

1. $3x^2+2x-6-4x^2+3x+2$

1. $2a^2-ab-b^2+4ab+3a^2+2b^2$ $\quad [b]$

1. $x^2-2ax^2y+4xy-3by+y^2+2xy-2by+4a$ $\quad [x と y], [y]$

**解答**

1. $-x^2+5x-4$

1. $5a^2+3ab+b^2$

3. $x^3 - 2ax^2y +6xy + y^2 -5by +4a$
$x$ と $y$ に着目すると $\quad$ 次数 3, 定数項 $4a$
更に、$y$に着目すると $y^2+(-2ax^2+6x-5b)y+x^2+4a$ より
$\quad$ 次数 2, 定数項 $x^2+4a$

# 多項式の加法・減法と乗法

多項式の計算における基本法則

$
\begin{array}{lccc}
&加法  &|& 乗法 \\
交換法則 & A+B=B+A &|& AB=BA \\
結合法則 & (A+B)+C=A+(B+C) &|& (AB)C = A(BC)
\end{array}
$

分配法則 $\quad A(B+C)=AB+AC, (A+B)C = AC+BC$

1. 指数法則

  - $a^ma^n= a^{m+n}$
  - $(a^m)^n = a^{mn}$
  - $(ab)^n = a^n b^n$

**展開の公式**

$(a+b)^2 = a^2+2ab+b^2$

$(a-b)^2 = a^2-2ab+b^2$

$(a+b)(a-b)=a^2-b^2$

$(x+a)(x+b)=x^2+(a+b)x+ab$

$(ax+b)(cx+d)=acx^2+(ad+bc)x+bd$

$(a+b)(a^2-ab+b^2)=a^3+b^3$

$(a-b)(a^2+ab+b^2)=a^3-b^3$

$(a+b)^3=a^3+3a^2b+3ab^2+b^3$

$(a-b)^3=a^3-3a^2b+3ab^2-b^3$

```
from sympy import *
a,b=symbols("a,b")
exp=(a+b)
exp2=(a-b)
display(exp**3)
display(expand(exp**3))
display(exp2**3)
display(expand(exp2**3))
display(exp*exp2)
display(expand(exp*exp2))
```

$\displaystyle \left(a + b\right)^{3}$

$\displaystyle a^{3} + 3 a^{2} b + 3 a b^{2} + b^{3}$

$\displaystyle \left(a - b\right)^{3}$

$\displaystyle a^{3} - 3 a^{2} b + 3 a b^{2} - b^{3}$

$\displaystyle \left(a - b\right) \left(a + b\right)$

$\displaystyle a^{2} - b^{2}$

```
# 基本例題 p.13
from sympy.abc import *
from sympy import *
A = x**2+3*y**2 - 2*x*y
display(A)
B=y**2+3*x*y - 2*x**2
display(B)
C=-3*x**2+x*y - 4*y**2
display(C)
print()
display(A+B)
display(A-B)
display(-3*A+2*B-C)
display(3*(2*A+C)-2*(2*(A+C)-(B-C)))
```

$\displaystyle x^{2} - 2 x y + 3 y^{2}$

$\displaystyle - 2 x^{2} + 3 x y + y^{2}$

$\displaystyle - 3 x^{2} + x y - 4 y^{2}$

$\displaystyle - x^{2} + x y + 4 y^{2}$

$\displaystyle 3 x^{2} - 5 x y + 2 y^{2}$

$\displaystyle - 4 x^{2} + 11 x y - 3 y^{2}$

$\displaystyle 7 x^{2} - x y + 20 y^{2}$

```
# 基本例題 3 (1) p.14
from sympy.abc import *
from sympy import *
expr1 = (-x*y**2)**2
expr2 = (-3*x**2*y)
display (expr1)
display (expr2)
print()
display(expr1*expr2)
```

$\displaystyle x^{2} y^{4}$

$\displaystyle - 3 x^{2} y$

$\displaystyle - 3 x^{4} y^{5}$

```
# 基本例題 3 (2) p.14
from sympy.abc import *
from sympy import *
expr1 = -a**2*b
expr2 = (-3*a**2*b*c**2)**3
display (expr1)
display (expr2)
print()
display(expr1*expr2)
```

$\displaystyle - a^{2} b$

$\displaystyle - 27 a^{6} b^{3} c^{6}$

$\displaystyle 27 a^{8} b^{4} c^{6}$

```
# 基本例題 3 (3) p.14
from sympy.abc import *
from sympy import *
expr1 = 3*a*b*c
expr2 = a+4*b-2*c
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle 3 a b c$

$\displaystyle a + 4 b - 2 c$

$\displaystyle 3 a^{2} b c + 12 a b^{2} c - 6 a b c^{2}$

```
# 基本例題 3 (4) p.14
from sympy.abc import *
from sympy import *
expr1 = (-x*y)**2
expr2 = 3*x**2 - 2*y -4
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle x^{2} y^{2}$

$\displaystyle 3 x^{2} - 2 y - 4$

$\displaystyle 3 x^{4} y^{2} - 2 x^{2} y^{3} - 4 x^{2} y^{2}$

```
# 練習 (1) p.14
from sympy.abc import *
from sympy import *
expr1 = (-a*b)**2
expr2 = -2*a**3*b
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle a^{2} b^{2}$

$\displaystyle - 2 a^{3} b$

$\displaystyle - 2 a^{5} b^{3}$

```
# 練習 (2) p.14
from sympy.abc import *
from sympy import *
expr1 = -2*x**4*y**2*z**3
expr2 = -3*x**2*y**2*z**4
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle - 2 x^{4} y^{2} z^{3}$

$\displaystyle - 3 x^{2} y^{2} z^{4}$

$\displaystyle 6 x^{6} y^{4} z^{7}$

```
# 練習 (3) p.14
from sympy.abc import *
from sympy import *
expr1 = 2*a**2*b*c
expr2 = a-3*b**2+2*c
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle 2 a^{2} b c$

$\displaystyle a - 3 b^{2} + 2 c$

$\displaystyle 2 a^{3} b c - 6 a^{2} b^{3} c + 4 a^{2} b c^{2}$

```
# 練習 (4) p.14
from sympy.abc import *
from sympy import *
expr1 = (-2*x)**3
expr2 = 3*x**2-2*x+4
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle - 8 x^{3}$

$\displaystyle 3 x^{2} - 2 x + 4$

$\displaystyle - 24 x^{5} + 16 x^{4} - 32 x^{3}$

```
# 基本例題4 (1) p.15
from sympy.abc import *
from sympy import *
expr1 = 2*x-3
expr2 = x**2+3*x-4
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle 2 x - 3$

$\displaystyle x^{2} + 3 x - 4$

$\displaystyle 2 x^{3} + 3 x^{2} - 17 x + 12$

```
# 基本例題4 (2) p.15
from sympy.abc import *
from sympy import *
expr1 = 3*x**3-5*x**2+1
expr2 = 1-x+2*x**2
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle 3 x^{3} - 5 x^{2} + 1$

$\displaystyle 2 x^{2} - x + 1$

$\displaystyle 6 x^{5} - 13 x^{4} + 8 x^{3} - 3 x^{2} - x + 1$

```
# 練習4 (1) p.15
from sympy.abc import *
from sympy import *
expr1 = 2*a+3*b
expr2 = a-2*b
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle 2 a + 3 b$

$\displaystyle a - 2 b$

$\displaystyle 2 a^{2} - a b - 6 b^{2}$

```
# 練習4 (2) p.15
from sympy.abc import *
from sympy import *
expr1 = 2*x-3*y-1
expr2 = 2*x-y-3
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle 2 x - 3 y - 1$

$\displaystyle 2 x - y - 3$

$\displaystyle 4 x^{2} - 8 x y - 8 x + 3 y^{2} + 10 y + 3$

```
# 練習4 (3) p.15
from sympy.abc import *
from sympy import *
expr1 = 2*a-3*b
expr2 = a**2+4*b**2-3*a*b
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle 2 a - 3 b$

$\displaystyle a^{2} - 3 a b + 4 b^{2}$

$\displaystyle 2 a^{3} - 9 a^{2} b + 17 a b^{2} - 12 b^{3}$

```
# 練習4 (4) p.15
from sympy.abc import *
from sympy import *
expr1 = 3*x+x**3-1
expr2 = 2*x**2-x-6
display (expr1)
display (expr2)
print()
display(expand(expr1*expr2))
```

$\displaystyle x^{3} + 3 x - 1$

$\displaystyle 2 x^{2} - x - 6$

$\displaystyle 2 x^{5} - x^{4} - 5 x^{2} - 17 x + 6$

```
# 基本例題 5 (1) p.16
from sympy.abc import *
from sympy import *
expr = (a+2)**2
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(a + 2\right)^{2}$

$\displaystyle a^{2} + 4 a + 4$

```
# 基本例題 5 (2) p.16
from sympy.abc import *
from sympy import *
expr = (3*x-4*y)**2
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(3 x - 4 y\right)^{2}$

$\displaystyle 9 x^{2} - 24 x y + 16 y^{2}$

```
# 基本例題 5 (3) p.16
from sympy.abc import *
from sympy import *
expr = (2*a+b)*(2*a-b)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(2 a - b\right) \left(2 a + b\right)$

$\displaystyle 4 a^{2} - b^{2}$

```
# 基本例題 5 (3) p.16
from sympy.abc import *
from sympy import *
expr = (x+3)*(x-5)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(x - 5\right) \left(x + 3\right)$

$\displaystyle x^{2} - 2 x - 15$

```
# 基本例題 5 (5) p.16
from sympy.abc import *
from sympy import *
expr = (2*x+3)*(3*x+4)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(2 x + 3\right) \left(3 x + 4\right)$

$\displaystyle 6 x^{2} + 17 x + 12$

```
# 基本例題 5 (6) p.16
from sympy.abc import *
from sympy import *
expr = (4*x+y)*(7*y-3*x)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(- 3 x + 7 y\right) \left(4 x + y\right)$

$\displaystyle - 12 x^{2} + 25 x y + 7 y^{2}$

```
# 基本例題 6 (1) p.17  次の式を展開する
from sympy.abc import *
from sympy import *
expr = (x+3)*(x**2-3*x+9)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(x + 3\right) \left(x^{2} - 3 x + 9\right)$

$\displaystyle x^{3} + 27$

```
# 基本例題 6 (2) p.17  次の式を展開する
from sympy.abc import *
from sympy import *
expr = (3*a-2*b)*(9*a**2+6*a*b+4*b**2)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(3 a - 2 b\right) \left(9 a^{2} + 6 a b + 4 b^{2}\right)$

$\displaystyle 27 a^{3} - 8 b^{3}$

```
# 基本例題 6 (3) p.17  次の式を展開する
from sympy.abc import *
from sympy import *
expr = (a+3)**3
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(a + 3\right)^{3}$

$\displaystyle a^{3} + 9 a^{2} + 27 a + 27$

```
# 基本例題 6 (4) p.17  次の式を展開する
from sympy.abc import *
from sympy import *
expr = (2*x-y)**3
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(2 x - y\right)^{3}$

$\displaystyle 8 x^{3} - 12 x^{2} y + 6 x y^{2} - y^{3}$

```
# 基本例題 7 p.18 次の式を展開する (1)
from sympy.abc import *
from sympy import *
expr = (a+b+c)**2
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(a + b + c\right)^{2}$

$\displaystyle a^{2} + 2 a b + 2 a c + b^{2} + 2 b c + c^{2}$

```
# 基本例題 7 p.18 次の式を展開する (2)
from sympy.abc import *
from sympy import *
expr = (x+y+z)*(x-y-z)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(x - y - z\right) \left(x + y + z\right)$

$\displaystyle x^{2} - y^{2} - 2 y z - z^{2}$

```
# 基本例題 7 p.18 次の式を展開する (3)
from sympy.abc import *
from sympy import *
expr = (x**2+3*x-2)*(x**2+3*x+3)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(x^{2} + 3 x - 2\right) \left(x^{2} + 3 x + 3\right)$

$\displaystyle x^{4} + 6 x^{3} + 10 x^{2} + 3 x - 6$

```
# 基本例題 8 p.19 次の式を展開する (1)
from sympy.abc import *
from sympy import *
expr = (x+y)*(x**2+y**2)*(x-y)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(x - y\right) \left(x + y\right) \left(x^{2} + y^{2}\right)$

$\displaystyle x^{4} - y^{4}$

```
# 基本例題 8 p.19 次の式を展開する (2)
from sympy.abc import *
from sympy import *
expr = (p+2*q)**2*(p-2*q)**2
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(p - 2 q\right)^{2} \left(p + 2 q\right)^{2}$

$\displaystyle p^{4} - 8 p^{2} q^{2} + 16 q^{4}$

```
# 基本例題 8 p.19 次の式を展開する (3)
from sympy.abc import *
from sympy import *
expr = (x+1)*(x-2)*(x**2-x+1)*(x**2+2*x+4)
display (expr)
print()
display(expand(expr))
```

$\displaystyle \left(x - 2\right) \left(x + 1\right) \left(x^{2} - x + 1\right) \left(x^{2} + 2 x + 4\right)$

$\displaystyle x^{6} - 7 x^{3} - 8$

# 因数分解

1. 2次式の因数分解の公式
$a^2+2ab+b^2=(a+b)^2$
$a^2-2ab+b^2=(a-b)^2$
$a^2-b^2=(a+b)(a-b)$
$x^2+(a+b)x+ab = (x+a)(x+b)$
$acx^2+(ad+bc)x+bd=(ax+b)(cx+d)$

2. 3次式の因数分解の公式
$a^3+b^3=(a+b)(a^2-ab+b^2)$
$a^3-b^3=(a+b)(a^2+ab+b^2)$
$a^3+3a^2b+3ab^2+b^3=(a+b)^3$
$a^3-3a^2b+3ab^2-b^3=(a-b)^3$

```
# 基本例題 9 p.22 (1) 次の式を因数分解する
from sympy.abc import *
from sympy import *
expr = 9*a**3*x**2*y-45*a*x**3*y**2+18*a**2*x*y**3
display (expr)
print()
display(simplify(factor(expr)))
```

$\displaystyle 9 a^{3} x^{2} y + 18 a^{2} x y^{3} - 45 a x^{3} y^{2}$

$\displaystyle 9 a x y \left(a^{2} x + 2 a y^{2} - 5 x^{2} y\right)$

```
# 項が思ったとおりに並ばない時に試すこと。

# init_printing(order='grlex')

# lex, revlex, grlex, grevlex, とか。

# simplify が有効な場合も多い
```

```
# 基本例題 9 p.22 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = (x-y)**2+y*z-z*x
display (expr)
print()
display(factor(expr))
```

$\displaystyle - x z + y z + \left(x - y\right)^{2}$

$\displaystyle \left(x - y\right) \left(x - y - z\right)$

```
# 基本例題 9 p.22 次の式を因数分解する (3)
from sympy.abc import *
from sympy import *
expr = x**2+14*x+49
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{2} + 14 x + 49$

$\displaystyle \left(x + 7\right)^{2}$

```
# 基本例題 9 p.22 次の式を因数分解する (4)
from sympy.abc import *
from sympy import *
expr = 9*x**2-12*x*y+4*y**2
display (expr)
print()
display(factor(expr))
```

$\displaystyle 9 x^{2} - 12 x y + 4 y^{2}$

$\displaystyle \left(3 x - 2 y\right)^{2}$

```
# 基本例題 9 p.22 次の式を因数分解する (5)
from sympy.abc import *
from sympy import *
expr = 6*a**3*b-24*a*b**3
display (expr)
print()
display(factor(expr))
```

$\displaystyle 6 a^{3} b - 24 a b^{3}$

$\displaystyle 6 a b \left(a - 2 b\right) \left(a + 2 b\right)$

```
# 基本例題 9 p.22 次の式を因数分解する (6)
from sympy.abc import *
from sympy import *
expr = x**2+7*x+10
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{2} + 7 x + 10$

$\displaystyle \left(x + 2\right) \left(x + 5\right)$

```
# 基本例題 9 p.22 次の式を因数分解する (7)
from sympy.abc import *
from sympy import *
expr = a**2+5*a-24
display (expr)
print()
display(factor(expr))
```

$\displaystyle a^{2} + 5 a - 24$

$\displaystyle \left(a - 3\right) \left(a + 8\right)$

```
# 基本例題 9 p.22 次の式を因数分解する (8)
from sympy.abc import *
from sympy import *
expr = 3*x**2 +5*x +2
display (expr)
print()
display(factor(expr))
```

$\displaystyle 3 x^{2} + 5 x + 2$

$\displaystyle \left(x + 1\right) \left(3 x + 2\right)$

```
# 基本例題 9 p.22 次の式を因数分解する (9)
from sympy.abc import *
from sympy import *
expr = 12*x**2 -16*x -3
display (expr)
print()
display(factor(expr))
```

$\displaystyle 12 x^{2} - 16 x - 3$

$\displaystyle \left(2 x - 3\right) \left(6 x + 1\right)$

```
# 基本例題 9 p.22 次の式を因数分解する (10)
from sympy.abc import *
from sympy import *
expr = 6*a**2+23*a*b-48*b**2
display (expr)
print()
display(factor(expr))
```

$\displaystyle 6 a^{2} + 23 a b - 48 b^{2}$

$\displaystyle \left(2 a - 3 b\right) \left(3 a + 16 b\right)$

```
# 基本例題 10 p. 23 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = x**3-27
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{3} - 27$

$\displaystyle \left(x - 3\right) \left(x^{2} + 3 x + 9\right)$

```
# 基本例題 10 p. 23 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = 64*a**3+125*b**3
display (expr)
print()
display(factor(expr))
```

$\displaystyle 64 a^{3} + 125 b^{3}$

$\displaystyle \left(4 a + 5 b\right) \left(16 a^{2} - 20 a b + 25 b^{2}\right)$

```
# 基本例題 10 p. 23 次の式を因数分解する (3)
from sympy.abc import *
from sympy import *
expr = x**3+6*x**2+12*x+8
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{3} + 6 x^{2} + 12 x + 8$

$\displaystyle \left(x + 2\right)^{3}$

```
# 基本例題 10 p. 23 次の式を因数分解する (4)
from sympy.abc import *
from sympy import *
expr = x**3+x**2-4*x-4
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{3} + x^{2} - 4 x - 4$

$\displaystyle \left(x - 2\right) \left(x + 1\right) \left(x + 2\right)$

```
# 基本例題 11 p. 24 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = 2*(x-1)**2-11*(x-1)+15
display (expr)
print()
display(factor(expr))
```

$\displaystyle - 11 x + 2 \left(x - 1\right)^{2} + 26$

$\displaystyle \left(x - 4\right) \left(2 x - 7\right)$

```
# 基本例題 11 p. 24 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = x**2-y**2+4*y-4
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{2} - y^{2} + 4 y - 4$

$\displaystyle \left(x - y + 2\right) \left(x + y - 2\right)$

```
# 基本例題 11 p. 24 次の式を因数分解する (3)
from sympy.abc import *
from sympy import *
expr = x**4-10*x**2+9
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{4} - 10 x^{2} + 9$

$\displaystyle \left(x - 3\right) \left(x - 1\right) \left(x + 1\right) \left(x + 3\right)$

```
# 基本例題 11 p. 24 次の式を因数分解する (4)
from sympy.abc import *
from sympy import *
expr = (x**2+3*x)**2-2*(x**2+3*x)-8
display (expr)
print()
display(factor(expr))
```

$\displaystyle - 2 x^{2} - 6 x + \left(x^{2} + 3 x\right)^{2} - 8$

$\displaystyle \left(x - 1\right) \left(x + 1\right) \left(x + 2\right) \left(x + 4\right)$

```
# 基本例題 12 p. 25 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = 9*b**2+3*a*b-2*a-4 #=> 次数の小さい文字 a について整理する
display (expr)
print()
display(factor(expr))
```

$\displaystyle 3 a b - 2 a + 9 b^{2} - 4$

$\displaystyle \left(3 b - 2\right) \left(a + 3 b + 2\right)$

```
# 基本例題 12 p. 25 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = x**3-x**2*y-x*z**2+y*z**2
#=> 次数の小さい文字 y について整理する
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{3} - x^{2} y - x z^{2} + y z^{2}$

$\displaystyle \left(x - y\right) \left(x - z\right) \left(x + z\right)$

```
# 基本例題 12 p. 25 次の式を因数分解する (3)
from sympy.abc import *
from sympy import *
expr = 1+2*a*b+a+2*b
display (expr)
print()
display(factor(expr))
```

$\displaystyle 2 a b + a + 2 b + 1$

$\displaystyle \left(a + 1\right) \left(2 b + 1\right)$

```
# 基本例題 13 p. 26 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = x**2-x*y-2*y**2-x-7*y-6
display (expr)
print()
display(factor(expr))
```

$\displaystyle x^{2} - x y - x - 2 y^{2} - 7 y - 6$

$\displaystyle \left(x - 2 y - 3\right) \left(x + y + 2\right)$

```
# 基本例題 13 p. 26 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = 3*x**2+7*x*y+2*y**2-5*x-5*y+2
display (expr)
print()
display(factor(expr))
```

$\displaystyle 3 x^{2} + 7 x y - 5 x + 2 y^{2} - 5 y + 2$

$\displaystyle \left(x + 2 y - 1\right) \left(3 x + y - 2\right)$

```
# 基本例題 14 p. 27 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = a**2*b+a*b**2+b**2*c+b*c**2+c**2*a+c*a**2+2*a*b*c
display(expr)
print()
display(factor(expr))
```

$\displaystyle a^{2} b + a^{2} c + a b^{2} + 2 a b c + a c^{2} + b^{2} c + b c^{2}$

$\displaystyle \left(a + b\right) \left(a + c\right) \left(b + c\right)$

```
# 基本例題 14 p. 27 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = a**2*(b-c)+b**2*(c-a)+c**2*(a-b)
display(expr)
print()
display(factor(expr))
```

$\displaystyle a^{2} \left(b - c\right) + b^{2} \left(- a + c\right) + c^{2} \left(a - b\right)$

$\displaystyle \left(a - b\right) \left(a - c\right) \left(b - c\right)$

```
# 基本例題 15 p. 30 次の式を計算する (1)
from sympy.abc import *
from sympy import *
expr = (x-1)*(x-2)*(x-3)*(x-4)
display(expr)
print()
display(expand(expr))
```

$\displaystyle \left(x - 4\right) \left(x - 3\right) \left(x - 2\right) \left(x - 1\right)$

$\displaystyle x^{4} - 10 x^{3} + 35 x^{2} - 50 x + 24$

```
# 基本例題 15 p. 30 次の式を計算する (2)
from sympy.abc import *
from sympy import *
expr = (a+b+c)**2+(b+c-a)**2+(c+a-b)**2+(a+b-c)**2
display(expr)
print()
display(expand(expr))
```

$\displaystyle \left(- a + b + c\right)^{2} + \left(a - b + c\right)^{2} + \left(a + b - c\right)^{2} + \left(a + b + c\right)^{2}$

$\displaystyle 4 a^{2} + 4 b^{2} + 4 c^{2}$

```
# 基本例題 15 p. 30 次の式を計算する (3)
from sympy.abc import *
from sympy import *
expr = (x+y)**4-(x-y)**4
display(expr)
print()
display(expand(expr))
```

$\displaystyle - \left(x - y\right)^{4} + \left(x + y\right)^{4}$

$\displaystyle 8 x^{3} y + 8 x y^{3}$

```
# 基本例題 16 p. 31 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = a**2*(b+c)+b**2*(c+a)+c**2*(a+b)+3*a*b*c
display(expr)
print()
display(factor(expr))
```

$\displaystyle a^{2} \left(b + c\right) + 3 a b c + b^{2} \left(a + c\right) + c^{2} \left(a + b\right)$

$\displaystyle \left(a + b + c\right) \left(a b + a c + b c\right)$

```
# 基本例題 16 p. 31 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = a**3*(b-c)+b**3*(c-a)+c**3*(a-b)
display(expr)
print()
display(factor(expr))
```

$\displaystyle a^{3} \left(b - c\right) + b^{3} \left(- a + c\right) + c^{3} \left(a - b\right)$

$\displaystyle \left(a - b\right) \left(a - c\right) \left(b - c\right) \left(a + b + c\right)$

```
# 基本例題 17 p. 32 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = (x**2+x-5)*(x**2+x-7)+1
display(expr)
print()
display(factor(expr))
```

$\displaystyle \left(x^{2} + x - 7\right) \left(x^{2} + x - 5\right) + 1$

$\displaystyle \left(x - 2\right)^{2} \left(x + 3\right)^{2}$

```
# 基本例題 17 p. 32 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = (a+b+c+1)*(a+1)+b*c
display(expr)
print()
display(factor(expr))
```

$\displaystyle b c + \left(a + 1\right) \left(a + b + c + 1\right)$

$\displaystyle \left(a + b + 1\right) \left(a + c + 1\right)$

```
# 基本例題 17 p. 32 次の式を因数分解する (3)
from sympy.abc import *
from sympy import *
expr = (x+1)*(x+2)*(x+3)*(x+4)-24
display(expr)
print()
display(factor(expr))
```

$\displaystyle \left(x + 1\right) \left(x + 2\right) \left(x + 3\right) \left(x + 4\right) - 24$

$\displaystyle x \left(x + 5\right) \left(x^{2} + 5 x + 10\right)$

```
# 基本例題 18 p. 33 次の式を因数分解する (1)
from sympy.abc import *
from sympy import *
expr = x**4+4*x**2+16
display(expr)
print()
display(factor(expr))
```

$\displaystyle x^{4} + 4 x^{2} + 16$

$\displaystyle \left(x^{2} - 2 x + 4\right) \left(x^{2} + 2 x + 4\right)$

```
# 基本例題 18 p. 33 次の式を因数分解する (2)
from sympy.abc import *
from sympy import *
expr = x**4-7*x**2*y**2+y**4
display(expr)
print()
display(factor(expr))
```

$\displaystyle x^{4} - 7 x^{2} y^{2} + y^{4}$

$\displaystyle \left(x^{2} - 3 x y + y^{2}\right) \left(x^{2} + 3 x y + y^{2}\right)$

```
# 基本例題 19 p. 34 (1)
# a**3+b**3 = (a+b)**3-3*a*b*(a+b) を用いて因数分解する
from sympy.abc import *
from sympy import *
expr = a**3+b**3+c**3-3*a*b*c
display(expr)
print()
display(factor(expr))
```

$\displaystyle a^{3} - 3 a b c + b^{3} + c^{3}$

$\displaystyle \left(a + b + c\right) \left(a^{2} - a b - a c + b^{2} - b c + c^{2}\right)$

```
# 実験
# a**3+b**3 = (a+b)**3-3*a*b*(a+b)
from sympy.abc import *
from sympy import *
expr1 = a**3+b**3
expr2 = (a+b)**3-3*a*b*(a+b)
expr3 = (a+b)**3
display(expr1)
display(expr2)
display(expr3)
print()
display(expand(expr1))
display(expand(expr2))
display(expand(expr3))
```

$\displaystyle a^{3} + b^{3}$

$\displaystyle - 3 a b \left(a + b\right) + \left(a + b\right)^{3}$

$\displaystyle \left(a + b\right)^{3}$

$\displaystyle a^{3} + b^{3}$

$\displaystyle a^{3} + b^{3}$

$\displaystyle a^{3} + 3 a^{2} b + 3 a b^{2} + b^{3}$

```
# 基本例題 19 p. 34 (2)
from sympy.abc import *
from sympy import *
expr = x**3+3*x*y+y**3-1
display(expr)
print()
display(factor(expr))
```

$\displaystyle x^{3} + 3 x y + y^{3} - 1$

$\displaystyle \left(x + y - 1\right) \left(x^{2} - x y + x + y^{2} + y + 1\right)$

# 実数

実数は有理数と無理数。

無理数は循環しない無限小数 $\quad (\sqrt 2 = 1.4142 \cdots \cdots)$ など。

有理数は整数と有限小数と循環小数。

有限小数と循環小数は分数になる。

整数は $\quad (0,\pm1,\pm2,\pm3,\cdots \cdots)$。

有限小数は $\quad \displaystyle \left (\frac12=0.5 など \right )$。

循環小数は $\quad \displaystyle \left (\frac13=0.333\cdots\cdots など \right )$。

循環小数と無理数は無限小数。

絶対値は数直線上で、原点 $\mathrm O$ と点 $\mathrm P(a)$ の間の距離を、実数 $a$ の絶対値と言い、記号 $|a|$ で表す。

$|a|= \left \{
\begin{array}{rl}
a & (a \geq 0 のとき)\\
-a & (a \lt 0 のとき)
\end{array}
\right .$

すべての実数は、数直線上の点で表される。

```
# 基本例題 20 p.37 (1) 循環少数を分数で表す
from sympy.abc import *
from sympy import *
display(Rational(2,3))
display(Rational(2,3).evalf())
display(nsimplify(0.66666666666666))
display()
display(nsimplify(1.383838383838383838))
```

$\displaystyle \frac{2}{3}$

$\displaystyle 0.666666666666667$

$\displaystyle \frac{2}{3}$

$\displaystyle \frac{137}{99}$

基本例題 20 p.37

(2) 数直線上において、2点間の距離を求める

(ア) $\mathrm P (2), \mathrm Q (5)$ #=> 5-2 => 3

(イ) $\mathrm A (2), \mathrm B (-3)$ #=> -3-2 => |-5| => 5

(ウ) $\mathrm C (-6), \mathrm D (-2)$ #=> -2-(-6) => 4

```
# 基本例題 20 p.37 (3) 絶対値を求める
from sympy import *
display(Abs(-7+3))
display(Abs(-2)+Abs(4))
display(Abs(6)-Abs(-4))

```

$\displaystyle 4$

$\displaystyle 6$

$\displaystyle 2$

# 平方根

平方根とは 2乗すると $a$ になる数を、$a$ の平方根と言う。 square root , sqrt

性質

$\quad a \geq 0 のとき \quad (\sqrt a)^2=a, \quad (-\sqrt a)^2=a, \quad \sqrt a \geq 0$
$\quad a \geq 0 のとき \quad \sqrt {a^2}=a$
$\quad a \leq 0 のとき \quad \sqrt {a^2} = -a$
$\quad すなわち \quad \sqrt {a^2} = |a|$

公式 $\quad a\geq0,\quad b\geq0,\quad k\geq0$ のとき

$\quad \sqrt a \sqrt b = \sqrt{ab}$
$\quad \displaystyle \frac{\sqrt a}{\sqrt b} = \sqrt{\frac a b}$
$\quad \sqrt {k^2a}= k \sqrt a$

**分母の有理化**

分母に根号を含む式を変形して、分母に根号が含まれない式にすることを、分母を有理化する、と言う。

25 の平方根は $\pm5$

```
from sympy.abc import *
from sympy import *
# ひと夜ひと夜に人見ごろ
display(sqrt(2))
display(sqrt(2).evalf())
# 人なみにおごれや
display(sqrt(3))
display(sqrt(3).evalf())
# 富士山麓にオーム鳴く
display(sqrt(5))
display(sqrt(5).evalf())
# 菜に無視いない
display(sqrt(7))
display(sqrt(7).evalf())
```

$\displaystyle \sqrt{2}$

$\displaystyle 1.4142135623731$

$\displaystyle \sqrt{3}$

$\displaystyle 1.73205080756888$

$\displaystyle \sqrt{5}$

$\displaystyle 2.23606797749979$

$\displaystyle \sqrt{7}$

$\displaystyle 2.64575131106459$

```
# 基本例題 21 p.39 次の値を求める (1)(a)
from sympy.abc import *
from sympy import *
expr = sqrt((-5)**2)
display(expr)
print()
display(expr)
```

$\displaystyle 5$

$\displaystyle 5$

```
# 基本例題 21 p.39 次の値を求める (1)(i)
from sympy.abc import *
from sympy import *
expr = sqrt((-8)*(-2))
display(expr)
print()
display(expr)
```

$\displaystyle 4$

$\displaystyle 4$

```
# 基本例題 21 p.39 次の値を求める (1)(u)
from sympy.abc import *
from sympy import *
expr = sqrt(a**2*b**2) # (a > 0, b < 0)
display(expr)
print()
display(-a*b)
```

$\displaystyle \sqrt{a^{2} b^{2}}$

$\displaystyle - a b$

```
# 基本例題 21 p.39 次の式を計算する (2)(a)
from sympy.abc import *
from sympy import *
expr = sqrt(12)+sqrt(27)-sqrt(48)
display(expr)
```

$\displaystyle \sqrt{3}$

```
# 基本例題 21 p.39 次の式を計算する (2)(i)
from sympy.abc import *
from sympy import *
expr = (sqrt(11)-sqrt(3))*(sqrt(11)+sqrt(3))
display(expr)
print()
display(expand(expr))
display(expr.evalf())
```

$\displaystyle \left(- \sqrt{3} + \sqrt{11}\right) \left(\sqrt{3} + \sqrt{11}\right)$

$\displaystyle 8$

$\displaystyle 8.0$

```
# 基本例題 21 p.39 次の式を計算する (2)(u)
from sympy.abc import *
from sympy import *
expr = (2*sqrt(2)-sqrt(27))**2
display(expr)
print()
display(expand(expr))
```

$\displaystyle \left(- 3 \sqrt{3} + 2 \sqrt{2}\right)^{2}$

$\displaystyle 35 - 12 \sqrt{6}$

```
# 基本例題 21 p.39 次の式を計算する (2)(e)
from sympy.abc import *
from sympy import *
expr = (sqrt(2)+sqrt(3)+sqrt(5))*(sqrt(2)+sqrt(3)-sqrt(5))
display(expr)
print()
display(expand(expr))
```

$\displaystyle \left(\sqrt{2} + \sqrt{3} + \sqrt{5}\right) \left(- \sqrt{5} + \sqrt{2} + \sqrt{3}\right)$

$\displaystyle 2 \sqrt{6}$

```
# 基本例題 22 p.40 分母を有理化する (1)
from sympy.abc import *
from sympy import *
expr = 1 / (sqrt(3)+sqrt(2))
display(expr)
print()
display(radsimp(expr)) #=> radsimp is rationalize denominator simplify
```

$\displaystyle \frac{1}{\sqrt{2} + \sqrt{3}}$

$\displaystyle - \sqrt{2} + \sqrt{3}$

```
# 基本例題 22 p.40 分母を有理化する (2)
from sympy.abc import *
from sympy import *
expr = sqrt(5)/(sqrt(3)+1) - sqrt(3)/(sqrt(5) + sqrt(3))
display(expr)
print()
display(radsimp(expr))
```

$\displaystyle - \frac{\sqrt{3}}{\sqrt{3} + \sqrt{5}} + \frac{\sqrt{5}}{1 + \sqrt{3}}$

$\displaystyle \frac{3 - \sqrt{5}}{2}$

```
# 基本例題 22 p.40 分母を有理化する (3)
from sympy.abc import *
from sympy import *
expr = (1 + sqrt(2) - sqrt(3))/(1 - sqrt(2) + sqrt(3))
display(expr)
print()
display(radsimp(expr))
```

$\displaystyle \frac{- \sqrt{3} + 1 + \sqrt{2}}{- \sqrt{2} + 1 + \sqrt{3}}$

$\displaystyle \frac{- \sqrt{2} + \sqrt{6}}{2}$

```
# 基本例題 23 p.41
# x=, y= のとき x+y = , xy = である、というタイプの問題
from sympy.abc import *
from sympy import *
x =  (sqrt(3)-sqrt(2))/(sqrt(3)+sqrt(2))
y =  (sqrt(3)+sqrt(2))/(sqrt(3)-sqrt(2))
display(x, y)
print()
display(simplify(radsimp(x+y)))
display(x*y)
print()
display(simplify(x**2+y**2))
display(simplify(x**3+y**3))
display(simplify(x**5+y**5))
```

$\displaystyle \frac{- \sqrt{2} + \sqrt{3}}{\sqrt{2} + \sqrt{3}}$

$\displaystyle \frac{\sqrt{2} + \sqrt{3}}{- \sqrt{2} + \sqrt{3}}$

$\displaystyle 10$

$\displaystyle 1$

$\displaystyle 98$

$\displaystyle 970$

$\displaystyle 95050$

```
# 基本例題 24 p.42 x+1/x = sqrt(5) のとき (1)
from sympy.abc import *
from sympy import *
print(solve(x + 1/x - sqrt(5),x))
expr = x**2 + 1/x**2
display(expr)
print()
x =solve(x + 1/x - sqrt(5),x)[0]
expr = x**2 + 1/x**2
display(simplify(expr))
x =solve(x + 1/x - sqrt(5),x)[0]
expr = x**2 + 1/x**2
display(simplify(expr))
```

    [-1/2 + sqrt(5)/2, 1/2 + sqrt(5)/2]

$\displaystyle x^{2} + \frac{1}{x^{2}}$

$\displaystyle 3$

$\displaystyle 3$

```
# 基本例題 24 p.42 x+1/x = sqrt(5) のとき (2)
from sympy.abc import *
from sympy import *
print(solve(x + 1/x - sqrt(5),x))
expr = x**3 + 1/x**3
display(expr)
print()
x =solve(x + 1/x - sqrt(5),x)[0]
expr = x**3 + 1/x**3
display(simplify(expr))
x =solve(x + 1/x - sqrt(5),x)[0]
expr = x**3 + 1/x**3
display(simplify(expr))
```

    [-1/2 + sqrt(5)/2, 1/2 + sqrt(5)/2]

$\displaystyle x^{3} + \frac{1}{x^{3}}$

$\displaystyle 2 \sqrt{5}$

$\displaystyle 2 \sqrt{5}$

```
# 基本例題 24 p.42 x+1/x = sqrt(5) のとき (3)
from sympy.abc import *
from sympy import *
print(solve(x + 1/x - sqrt(5),x))
expr = x**4 + 1/x**4
display(expr)
print()
x =solve(x + 1/x - sqrt(5),x)[0]
expr = x**4 + 1/x**4
display(simplify(expr))
x =solve(x + 1/x - sqrt(5),x)[0]
expr = x**4 + 1/x**4
display(simplify(expr))
```

    [-1/2 + sqrt(5)/2, 1/2 + sqrt(5)/2]

$\displaystyle x^{4} + \frac{1}{x^{4}}$

$\displaystyle 7$

$\displaystyle 7$

# いまここ

```
# 基本問題 25 p.43
# sqrt(a-1)
```

# 最後