# 清风的技术小屋

0%

## 问题描述

Implement int sqrt(int x).

Compute and return the square root of $$x$$, where $$x$$ is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

Example 2:

Related Topics: Math, Binary Search

## 问题描述

Implement pow(x, n), which calculates $$x$$ raised to the power $$n$$ ($$x^n$$).

Example 1:

Example 2:

Example 3:

Note:

• $$-100.0 \lt x \lt 100.0$$
• $$n$$ is a 32-bit signed integer, within the range $$[-2^{31}, 2^{31}-1]$$

Related Topics: Math, Binary Search

## 解决方案

### 方案1

$x^n = \begin{cases} x^{n/2} \cdot x^{n/2} & n \text{ is even} \\ x^{n/2} \cdot x^{n/2} \cdot x & n \text{ is odd} \end{cases}$

$x^{n} = x^{n/2} \cdot x^{n/2} \cdot \frac{1}{x}$

### 方案2

$x^5 = x^{2^2} \cdot x^{2^0}$

$\text{ans} = \text{ans} \cdot x^{2^k}$

# 1. 安装环境

OS Version: OS X 10.11.6

# 1. 将 NumPy 导入为 np，并查看版本

English Version

Title: Import numpy as np and see the version

Difficulty Level: L1

Question: Import numpy as np and print the version number.

Solution

# 2. 如何创建 1 维数组？

English Version

Title: How to create a 1D array?

Difficulty Level: L1

Question: Create a 1D array of numbers from 0 to 9.

Solution

# 3. 如何创建 boolean 数组？

English Version

Title: How to create a boolean array?

Difficulty Level: L1

Question: Create a 3×3 numpy array of all True’s.

Solution 1
Solution 2

# 4. 如何从 1 维数组中提取满足给定条件的项？

English Version

Title: How to extract items that satisfy a given condition from 1D array?

Difficulty Level: L1

Question: Extract all odd numbers from arr.

Solution

# 5. 如何将 NumPy 数组中满足给定条件的项替换成另一个数值？

English Version

Title: How to replace items that satisfy a condition with another value in numpy array?

Difficulty Level: L1

Question: Replace all odd numbers in arr with -1.

Solution

# 6. 如何在不影响原始数组的前提下替换满足给定条件的项？

English Version

Title: How to replace items that satisfy a condition without affecting the original array?

Difficulty Level: L2

Question: Replace all odd numbers in arr with -1 without changing arr.

Solution 1
Solution 2

# 7. 如何重塑（reshape）数组？

English Version

Title: How to reshape an array?

Difficulty Level: L1

Question: Convert a 1D array to a 2D array with 2 rows.

Solution

# 8. 如何垂直堆叠两个数组？

English Version

Title: How to stack two arrays vertically?

Difficulty Level: L2

Question: Stack arrays a and b vertically.

Solution 1
Solution 2
Solution 3

# 9. 如何水平堆叠两个数组？

English Version

Title: How to stack two arrays horizontally?

Difficulty Level: L2

Question: Stack the arrays a and b horizontally.

Solution 1
Solution 2
Solution 3

# 10. 在不使用硬编码的前提下，如何在 NumPy 中生成自定义序列？

English Version

Title: How to generate custom sequences in numpy without hardcoding?

Difficulty Level: L2

Question: Create the following pattern without hardcoding. Use only numpy functions and the below input array a.

Solution 1
Solution 2

# 11. 如何获得两个 Python NumPy 数组中共同的项？

English Version

Title: How to get the common items between two python numpy arrays?

Difficulty Level: L2

Question: Get the common items between a and b.

Solution

# 12. 如何从一个数组中移除与另一个数组重复的项？

English Version

Title: How to remove from one array those items that exist in another?

Difficulty Level: L2

Question: From array a remove all items present in array b.

Solution

# 13. 如何获取两个数组匹配元素的位置？

English Version

Title: How to get the positions where elements of two arrays match?

Difficulty Level: L2

Question: Get the positions where elements of a and b match.

Solution

# 14. 如何从 NumPy 数组中提取给定范围内的所有数字？

English Version

Title: How to extract all numbers between a given range from a numpy array?

Difficulty Level: L2

Question: Get all items between 5 and 10 from a.

Solution 1
Solution 2
Solution 3

# 15. 如何创建一个 Python 函数以对 NumPy 数组执行元素级的操作？

English Version

Title: How to make a python function that handles scalars to work on numpy arrays?

Difficulty Level: L2

Question: Convert the function maxx that works on two scalars, to work on two arrays.

Solution

# 16. 如何在 2d NumPy 数组中交换两个列？

English Version

Title: How to swap two columns in a 2d numpy array?

Difficulty Level: L2

Question: Swap columns 1 and 2 in the array arr.

Solution 1
Solution 2

# 17. 如何在 2d NumPy 数组中交换两个行？

English Version

Title: How to swap two rows in a 2d numpy array?

Difficulty Level: L2

Question: Swap rows 1 and 2 in the array arr.

Solution 1
Solution 2

# 18. 如何反转 2D 数组的所有行？

English Version

Title: How to reverse the rows of a 2D array?

Difficulty Level: L2

Question: Reverse the rows of a 2D array arr.

Solution

# 19. 如何反转 2D 数组的所有列？

English Version

Title: How to reverse the columns of a 2D array?

Difficulty Level: L2

Question: Reverse the columns of a 2D array arr.

Solution

# 20. 如何创建一个包含 5 和 10 之间浮点数的随机 2 维数组？

English Version

Title: How to create a 2D array containing random floats between 5 and 10?

Difficulty Level: L2

Question: Create a 2D array of shape 5x3 to contain random decimal numbers between 5 and 10.

Solution 1
Solution 2
Solution 3

# 21. 如何在 Python NumPy 数组中仅输出小数点后三位的数字？

English Version

Title: How to print only 3 decimal places in python numpy array?

Difficulty Level: L1

Question: Print or show only 3 decimal places of the numpy array rand_arr.

Solution

# 22. 如何通过禁用科学计数法（如 1e10）打印 NumPy 数组？

English Version

Title: How to pretty print a numpy array by suppressing the scientific notation (like 1e10)?

Difficulty Level: L1

Question: Pretty print rand_arr by suppressing the scientific notation (like 1e10).

Solution

# 23. 如何限制 NumPy 数组输出中项的数目？

English Version

Title: How to limit the number of items printed in output of numpy array?

Difficulty Level: L1

Question: Limit the number of items printed in python numpy array a to a maximum of 6 elements.

Solution

# 24. 如何在不截断数组的前提下打印出完整的 NumPy 数组？

English Version

Title: How to print the full numpy array without truncating

Difficulty Level: L1

Question: Print the full numpy array a without truncating.

Solution 1
Solution 2

# 25. 如何向 Python NumPy 导入包含数字和文本的数据集，同时保持文本不变？

English Version

Title: How to import a dataset with numbers and texts keeping the text intact in python numpy?

Difficulty Level: L2

Question: Import the iris dataset keeping the text intact.

Iris Data Set 网页下载数据集 iris.data

Solution

Since we want to retain the species, a text field, I have set the dtype to object. Had I set dtype=None, a 1d array of tuples would have been returned.

# 26. 如何从 1 维元组数组中提取特定的列？

English Version

Title: How to extract a particular column from 1D array of tuples?

Difficulty Level: L2

Question: Extract the text column species from the 1D iris_1d.

Solution 1
Solution 2

# 27. 如何将 1 维元组数组转换成 2 维 NumPy 数组？

English Version

Title: How to convert a 1d array of tuples to a 2d numpy array?

Difficulty Level: L2

Question: Convert the 1D iris_1d to 2D array iris_2d by omitting the species text field.

Solution

# 28. 如何计算 NumPy 数组的平均值、中位数和标准差？

English Version

Title: How to compute the mean, median, standard deviation of a numpy array?

Difficulty: L1

Question: Find the mean, median, standard deviation of iris's sepal length (1st column).

Solution

# 29. 如何归一化数组，使值的范围在 0 和 1 之间？

English Version

Title: How to normalize an array so the values range exactly between 0 and 1?

Difficulty: L2

Question: Create a normalized form of iris's sepal length whose values range exactly between 0 and 1 so that the minimum has value 0 and maximum has value 1.

Solution

# 30. 如何计算 softmax 分数？

English Version

Title: How to compute the softmax score?

Difficulty Level: L3

Question: Compute the softmax score of sepal length.

Solution

According formula:

$S(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}$

For numerical stability, the formula changes to:

$S(x_i) = \frac{e^{(x_i - x_{max})}}{\sum_j e^{(x_j - x_{max})}}$

where $$x_{max} = max(x)$$.

# 31. 如何找到 NumPy 数组的百分数？

English Version

Title: How to find the percentile scores of a numpy array?

Difficulty Level: L1

Question: Find the 5th and 95th percentile of iris's sepal length.

Solution

# 32. 如何在数组的随机位置插入值？

English Version

Title: How to insert values at random positions in an array?

Difficulty Level: L2

Question: Insert np.nan values at 20 random positions in iris_2d dataset.

Solution 1
Solution 2

# 33. 如何在 NumPy 数组中找出缺失值的位置？

English Version

Title: How to find the position of missing values in numpy array?

Difficulty Level: L2

Question: Find the number and position of missing values in iris_2d's sepal length (1st column).

Solution 1
Solution 2

# 34. 如何基于两个或以上条件过滤 NumPy 数组？

English Version

Title: How to filter a numpy array based on two or more conditions?

Difficulty Level: L3

Question: Filter the rows of iris_2d that has petal length (3rd column) > 1.5 and sepal length (1st column) < 5.0.

Solution

# 35. 如何在 NumPy 数组中删除包含缺失值的行？

English Version

Title: How to drop rows that contain a missing value from a numpy array?

Difficulty Level: L3:

Question: Select the rows of iris_2d that does not have any nan value.

Solution 1
Solution 2

# 36. 如何找出 NumPy 数组中两列之间的关联性？

English Version

Title: How to find the correlation between two columns of a numpy array?

Difficulty Level: L2

Question: Find the correlation between sepal length(1st column) and petal length(3rd column) in iris_2d.

Solution 1
Solution 2

# 37. 如何确定给定数组是否有空值？

English Version

Title: How to find if a given array has any null values?

Difficulty Level: L2

Question: Find out if iris_2d has any missing values.

Solution 1
Solution 2

# 38. 如何在 NumPy 数组中将所有缺失值替换成0？

English Version

Title: How to replace all missing values with 0 in a numpy array?

Difficulty Level: L2

Question: Replace all ccurrences of nan with 0 in numpy array.

Solution

# 39. 如何在 NumPy 数组中找出唯一值的数量？

English Version

Title: How to find the count of unique values in a numpy array?

Difficulty Level: L2

Question: Find the unique values and the count of unique values in iris's species.

Solution

# 40. 如何将一个数值转换为一个类别（文本）数组？

English Version

Title: How to convert a numeric to a categorical (text) array?

Difficulty Level: L2

Question: Bin the petal length (3rd) column of iris_2d to form a text array, such that if petal length is:

Solution 1
Solution 2

# 41. 如何基于 NumPy 数组现有列创建一个新的列？

English Version

Title: How to create a new column from existing columns of a numpy array?

Difficulty Level: L2

Question: Create a new column for volume in iris_2d, where volume is (pi x petallength x sepal_length^2)/3.

Solution 1
Solution 2

# 42. 如何在 NumPy 中执行概率采样？

English Version

Title: How to do probabilistic sampling in numpy?

Difficulty Level: L3

Question: Randomly sample iris's species such that setosa is twice the number of versicolor and virginica.

Solution

# 43. 如何在多维数组中找到一维的第二最大值？

English Version

Title: How to get the second largest value of an array when grouped by another array?

Difficulty Level: L2

Question: What is the value of second longest petal length of species setosa

Solution

# 44. 如何用给定列将 2 维数组排序？

English Version

Title: How to sort a 2D array by a column

Difficulty Level: L2

Question: Sort the iris dataset based on sepal length column.

Solution

# 45. 如何在 NumPy 数组中找到最频繁出现的值？

English Version

Title: How to find the most frequent value in a numpy array?

Difficulty Level: L1

Question: Find the most frequent value of petal length (3rd column) in iris dataset.

Solution

# 46. 如何找到第一个大于给定值的数的位置？

English Version

Title: How to find the position of the first occurrence of a value greater than a given value?

Difficulty Level: L2

Question: Find the position of the first occurrence of a value greater than 1.0 in petal width 4th column of iris dataset.

Solution 1
Solution 2

# 47. 如何将数组中所有大于给定值的数替换为给定的 cutoff 值？

English Version

Title: How to replace all values greater than a given value to a given cutoff?

Difficulty Level: L2

Question: From the array a, replace all values greater than 30 to 30 and less than 10 to 10.

Solution 1
Solution 2
Solution 3

# 48. 如何在 NumPy 数组中找到 top-n 数值的位置？

English Version

Title: How to get the positions of top n values from a numpy array?

Difficulty Level: L2

Question: Get the positions of top 5 maximum values in a given array a.

Solution 1
Solution 2

# 49. 如何逐行计算数组中所有值的数量？

English Version

Title: How to compute the row wise counts of all possible values in an array?

Difficulty Level: L4

Question: Compute the counts of unique values row-wise.

Solution 1
Solution 2

# 50. 如何将 array_of_arrays 转换为平面 1 维数组？

English Version

Title: How to convert an array of arrays into a flat 1d array?

Difficulty Level: 2

Question: Convert array_of_arrays into a flat linear 1d array.

Solution 1
Solution 2

# 51. 如何为 NumPy 数组生成 one-hot 编码？

English Version

Title: How to generate one-hot encodings for an array in numpy?

Difficulty Level L4

Question: Compute the one-hot encodings (dummy binary variables for each unique value in the array).

Solution 1
Solution 2

# 52. 如何创建由类别变量分组确定的一维数值？

English Version

Title: How to create row numbers grouped by a categorical variable?

Difficulty Level: L3

Question: Create row numbers grouped by a categorical variable. Use the following sample from iris species as input.

Solution 1
Solution 2

# 53. 如何基于给定的类别变量创建分组 id？

English Version

Title: How to create groud ids based on a given categorical variable?

Difficulty Level: L4

Question: Create group ids based on a given categorical variable. Use the following sample from iris species as input.

Solution

# 54. 如何使用 NumPy 对数组中的项进行排序？

English Version

Title: How to rank items in an array using numpy?

Difficulty Level: L2

Question: Create the ranks for the given numeric array a.

Solution

# 55. 如何使用 NumPy 对多维数组中的项进行排序？

English Version

Title: How to rank items in a multidimensional array using numpy?

Difficulty Level: L3

Question: Create a rank array of the same shape as a given numeric array a.

Solution 1
Solution 2

# 56. 如何在 2 维 NumPy 数组中找到每一行的最大值？

English Version

Title: How to find the maximum value in each row of a numpy array 2d?

Difficulty Level: L2

Question: Compute the maximum for each row in the given array.

Solution 1
Solution 2

# 57. 如何计算 2 维 NumPy 数组每一行的 min-by-max？

English Version

Title: How to compute the min-by-max for each row for a numpy array 2d?

Difficulty Level: L3

Question: Compute the min-by-max for each row for given 2d numpy array.

Solution

# 58. 如何在 NumPy 数组中找到重复条目？

English Version

Title: How to find the duplicate records in a numpy array?

Difficulty Level: L3

Question: Find the duplicate entries (2nd occurrence onwards) in the given numpy array and mark them as True. First time occurrences should be False.

Solution

# 59. 如何找到 NumPy 的分组平均值？

English Version

Title: How to find the grouped mean in numpy?

Difficulty Level L3

Question: Find the mean of a numeric column grouped by a categorical column in a 2D numpy array.

Solution

# 60. 如何将 PIL 图像转换成 NumPy 数组？

English Version

Title: How to convert a PIL image to numpy array?

Difficulty Level: L3

Question: Import the image from the following url and convert it to a numpy array.

Solution

# 61. 如何删除 NumPy 数组中所有的缺失值？

English Version

Title: How to drop all missing values from a numpy array?

Difficulty Level: L2

Question: Drop all nan values from a 1D numpy array.

Solution

# 62. 如何计算两个数组之间的欧几里得距离？

English Version

Title: How to compute the euclidean distance between two arrays?

Difficulty Level: L1

Question: Compute the euclidean distance between two arrays a and b.

Solution 1
Solution 2

# 63. 如何在一个 1 维数组中找到所有的局部极大值（peak）？

English Version

Title: How to find all the local maxima (or peaks) in a 1d array?

Difficulty Level: L4

Question: Find all the peaks in a 1D numpy array a. Peaks are points surrounded by smaller values on both sides.

Solution

# 64. 如何从 2 维数组中减去 1 维数组，从 2 维数组的每一行分别减去 1 维数组的每一项？

English Version

Title: How to subtract a 1d array from a 2d array, where each item of 1d array subtracts from respective row?

Difficulty Level: L2

Question: Subtract the 1d array b_1d from the 2d array a_2d, such that each item of b_1d subtracts from respective row of a_2d.

Solution

# 65. 如何在数组中找出某个项的第 n 个重复索引？

English Version

Title: How to find the index of n'th repetition of an item in an array

Difficulty Level L2

Question: Find the index of 5th repetition of number 1 in x.

Solution 1
Solution 2
Solution 3

# 66. 如何将 NumPy 的 datetime64 对象（object）转换为 datetime 的 datetime 对象？

English Version

Title: How to convert numpy's datetime64 object to datetime's datetime object?

Difficulty Level: L2

Question: Convert numpy's datetime64 object to datetime's datetime object.

Solution 1
Solution 2

# 67. 如何计算 NumPy 数组的移动平均数？

English Version

Title: How to compute the moving average of a numpy array?

Difficulty Level: L3

Question: Compute the moving average of window size 3, for the given 1D array.

Solution 1
Solution 2

# 68. 给定起始数字、length 和步长，如何创建一个 NumPy 数组序列？

English Version

Title: How to create a numpy array sequence given only the starting point, length and the step?

Difficulty Level: L2

Question: Create a numpy array of length 10, starting from 5 and has a step of 3 between consecutive numbers.

Solution 1
Solution 2

# 69. 如何在不规则 NumPy 日期序列中填充缺失日期？

English Version

Title: How to fill in missing dates in an irregular series of numpy dates?

Difficulty Level: L3

Question: Given an array of a non-continuous sequence of dates. Make it a continuous sequence of dates, by filling in the missing dates.

Solution 1
Solution 2

# 70. 如何基于给定的 1 维数组创建 strides？

English Version

Title: How to create strides from a given 1D array?

Difficulty Level: L4

Question: From the given 1d array arr, generate a 2d matrix using strides, with a window length of 4 and strides of 2, like [[0,1,2,3], [2,3,4,5], [4,5,6,7]..]

Solution

# References

1. 容器继承collection.abc.Iterable，然后实现__iter__()方法
2. 实现一个Iterator类，继承自collection.abc.Iterator，然后实现__next__()方法

## 介绍

ConfigSpace是一个用于管理算法参数空间的Python包，主要用于算法参数选择任务。一些AutoML库，例如SMAC3BOHB以及auto-sklearn，会用到该包。项目主页为：https://github.com/automl/ConfigSpace

## 整数参数和浮点参数

1. C为惩罚参数，数据类型为浮点数，且$$C \ge 0$$
2. max_iter为最大迭代次数，数据类型为整数

## Categorical参数和参数之间的联系

sklearn.svm.SVC类介绍可知，算法核类型由参数kernel控制

• kernel限定算法的核类型，取值主要有'linear''poly''rbf''sigmoid'

• Linear核$$K(x, y)=x^Ty$$，无参数
• Poly核$$K(x, y)=(\gamma x^Ty + r)^d$$，其中参数$$\gamma$$对应gamma，参数$$r$$对应coef0，参数$$d$$对应degree
• RBF核$$K(x, y)=\exp(-\gamma \Vert x - y\Vert^2)$$，其中参数$$\gamma$$对应gamma
• Sigmoid核$$K(x, y)=\tanh(\gamma x^T y + r)$$，其中参数$$\gamma$$对应gamma，参数$$r$$对应coef0

• 参数degree关联Poly核
• 参数coef0关联Poly核和Sigmoid核
• 参数gamma关联Poly核、RBF核和Sigmoid核

## 禁止参数取值组合出现

LinearSVC类部分参数如下

• penalty设置正则项类型，数据类型为字符串，取值为'l1'或者'l2'
• loss设置损失函数类型，数据类型为字符串，取值为'hinge'或者'squared_hinge'
• dual设置算法是否求解对偶问题，数据类型为布尔值，实际可以替换成字符串类型

• 参数penalty取值'l1'，参数loss取值'hinge'
• 参数dual取值'False'，参数penalty取值'l2'，参数loss取值'hinge'
• 参数dual取值'False'，参数'penalty'取值'l1'

## 介绍

$\Omega = K(X, Y) = \begin{bmatrix} K(x_1, y_1) & K(x_1, y_2) & \cdots & K(x_1, y_n) \\ K(x_2, y_1) & K(x_2, y_2) & \cdots & K(x_2, y_n) \\ \vdots & \vdots & \ddots & \vdots \\ K(x_m, y_1) & K(x_n, y_2) & \cdots & K(x_m, y_n) \end{bmatrix}$

\begin{aligned} X & = [x_1 \, x_2 \, \cdots \, x_m]^T \\ Y & = [y_1 \, y_2 \, \cdots \, y_n]^T \end{aligned}

## 快速生成核矩阵

$K(x, y) = e^{-\frac{\Vert x - y \Vert^2}{2\sigma^2}}$

$\Omega=K(X, Y) = \begin{bmatrix} e^{-\frac{\Vert x_1 - y_1\Vert^2}{2\sigma^2}} & e^{-\frac{\Vert x_1 - y_2 \Vert^2}{2\sigma^2}} & \cdots & e^{-\frac{\Vert x_1 - y_n \Vert^2}{2\sigma^2}} \\ e^{-\frac{\Vert x_2 - y_1 \Vert^2}{2\sigma^2}} & e^{-\frac{\Vert x_2 - y_2 \Vert^2}{2\sigma^2}} & \cdots & e^{-\frac{\Vert x_2 - y_n \Vert^2}{2\sigma^2}} \\ \vdots & \vdots & \ddots & \vdots \\ e^{-\frac{\Vert x_m - y_1 \Vert^2}{2\sigma^2}} & e^{-\frac{\Vert x_m - y_2 \Vert^2}{2\sigma^2}} & \cdots & e^{-\frac{\Vert x_m - y_n \Vert^2}{2\sigma^2}} \end{bmatrix}$

$D = \begin{bmatrix} \Vert x_1 - y_1 \Vert^2 & \Vert x_1 - y_2 \Vert^2 & \cdots & \Vert x_1 - y_n \Vert^2 \\ \Vert x_2 - y_1 \Vert^2 & \Vert x_2 - y_2 \Vert^2 & \cdots & \Vert x_2 - y_n \Vert^2 \\ \vdots & \vdots & \ddots & \vdots \\ \Vert x_m - y_1 \Vert^2 & \Vert x_m - y_2 \Vert^2 & \cdots & \Vert x_m - y_n \Vert^2 \end{bmatrix}$

\begin{aligned} \Vert x_i - y_j \Vert^2 & = (x_i - y_j)^T(x_i - y_j) \\ & = x_i^T x_i - 2 x_i^T y_j + y_j^T y_j \\ & = \Vert x_i \Vert^2 - 2 x_i^T y_j + \Vert y_j \Vert^2 \end{aligned}

$D = A - 2B + C$

$A = \begin{bmatrix} \Vert x_1 \Vert^2 & \Vert x_1 \Vert^2 & \cdots & \Vert x_1 \Vert^2 \\ \Vert x_2 \Vert^2 & \Vert x_2 \Vert^2 & \cdots & \Vert x_2 \Vert^2 \\ \vdots & \vdots & \ddots & \vdots \\ \Vert x_m \Vert^2 & \Vert x_m \Vert^2 & \cdots & \Vert x_m \Vert^2 \end{bmatrix}_{m \times n}$

$\alpha = \begin{bmatrix} \Vert x_1 \Vert^2 & \Vert x_2 \Vert^2 & \cdots & \Vert x_m \Vert^2 \end{bmatrix}^T$

\begin{aligned} A & = \alpha 1^T \\ & = \begin{bmatrix} \Vert x_1 \Vert^2 \\ \Vert x_2 \Vert^2 \\ \vdots \\ \Vert x_m \Vert^2 \end{bmatrix} \begin{bmatrix} 1 & 1 & \cdots & 1 \end{bmatrix} \end{aligned}

$B = \begin{bmatrix} x_1^T y_1 & x_1^T y_2 & \cdots & x_1^T y_n \\ x_2^T y_1 & x_2^T y_2 & \cdots & x_2^T y_n \\ \vdots & \vdots & \ddots & \vdots \\ x_m^T y_1 & x_m^T y_2 & \cdots & x_m^T y_n \end{bmatrix}_{m \times n}$

$B = XY^T$

$C = \begin{bmatrix} \Vert y_1 \Vert^2 & \Vert y_2 \Vert^2 & \cdots & \Vert y_n \Vert^2 \\ \Vert y_1 \Vert^2 & \Vert y_2 \Vert^2 & \cdots & \Vert y_n \Vert^2 \\ \vdots & \vdots & \ddots & \vdots \\ \Vert y_1 \Vert^2 & \Vert y_2 \Vert^2 & \cdots & \Vert y_n \Vert^2 \end{bmatrix}_{m \times n}$

$\beta = \begin{bmatrix} \Vert y_1 \Vert^2 & \Vert y_2 \Vert & \cdots & \Vert y_n \Vert^2 \end{bmatrix}^T$

\begin{aligned} C & = 1 \beta^T \\ & = \begin{bmatrix} 1 \\ 1 \\ \vdots \\ 1 \end{bmatrix} \begin{bmatrix} \Vert y_1 \Vert^2 & \Vert y_2 \Vert & \cdots & \Vert y_n \Vert^2 \end{bmatrix} \end{aligned}

$D = \alpha 1^T - 2 XY^T + 1 \beta^T$

## 保存和恢复

• prefix + Ctrl-s - save
• prefix + Ctrl-r - restore

## BLAS

BLAS，全称Basic Linear Algebra Subprograms，即基础线性代数子程序库，里面有大量已经编写好的关于线性代数运算的程序。BLAS库的API分三大类，分别为Level 1Level 2以及Level 3

• Level 1: 函数处理单一向量的线性运算以及两个向量的二元运算
• Level 2: 函数处理矩阵与向量的运算，同时也包含线性方程组求解
• Level 3: 函数处理矩阵与矩阵之间的运算

API的全部介绍可以参考官方文档：blasqr.pdf

## Cython调用BLAS函数

Cython要调用BLAS函数，主要是通过BLAS库的C接口，接口的具体定义可以参考现有BLAS库的 cblas.h，里面函数的命名和 blasqr.pdf 中函数命名是一一对应的，只不过函数前多了cblas_前缀。这里演示如何调用函数cblas_dgemv，在头文件 cblas.h 中函数声明为

$y = \alpha Ax + \beta y$