【LaTeX】 案例分析 (5) - 与 LaTeX 斗智斗勇

这是SEU新生研讨课作业,主要内容是学习数字信号处理。


代码

\documentclass{article}

\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{listings}
\usepackage{appendix}
\usepackage{bm, amsmath, amsfonts}
\usepackage{multirow}
\usepackage{hyperref}
\usepackage{subfig}
\usepackage{url}
\usepackage{cite}
%\usepackage{subfigure}
\usepackage[a4paper, left=2.5cm, right=2.5cm, top=2.65cm, bottom=2.54cm]{geometry}

\title{\Huge \bfseries Calculation of minimum number of registers with life time chart}
\author{\Large Teddy van Jerry}
\date{\today}

\pagestyle{fancy}
\fancyhf{}
\cfoot{\thepage}
\chead{ALL RIGHTS RESERVED \copyright \  2020 Teddy van Jerry }

% set the code style
\RequirePackage{listings}
\RequirePackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{
	numbers=left,  
	frame=tb,
	aboveskip=3mm,
	belowskip=3mm,
	showstringspaces=false,
	columns=flexible,
	framerule=1pt,
	rulecolor=\color{gray!35},
	backgroundcolor=\color{gray!5},
	basicstyle={\ttfamily},
	numberstyle=\tiny\color{gray},
	keywordstyle=\color{blue},
	commentstyle=\color{dkgreen},
	stringstyle=\color{mauve},
	breaklines=true,
	breakatwhitespace=true,
	tabsize=3,
}

\begin{document}

    \maketitle

    \section{The exercise}

        \ \ \ \ To transpose a fourth-order matrix,
        what is the minimum number of registers?\cite{VLSI}

    \section{Life time chart}

        \ \ \ \ To calculate the minimum number of registers,
        I use C++ to work out the answer
        and draw the life time chart at the same time.\cite{Lifetime}

        Firstly we know the delay matrix
        \footnote{This is also calculated by C++.}
        is:

        $$
        \left[ \begin{matrix}
            9 & 12 & 15 & 18 \\
            6 &  9 & 12 & 15 \\
            3 &  6 &  9 & 12 \\
            0 &  3 &  6 &  9 \\
        \end{matrix} \right]
        $$

        Then we can draw (or calculate) the life time chart
        using C++.

        \begin{lstlisting}[language=C++,escapeinside=``]
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
        
vector<vector<char>> Mat_define(int n)
{
    vector<vector<char>> mat;
    for (size_t i = 0; i != n; i++)
    {
        vector<char> row;
        for (size_t j = 0; j != n; j++)
        {
            row.push_back('a' + n * i + j);
        }
        mat.push_back(row);
    }
    return mat;
}

vector<vector<char>> transpose(vector<vector<char>> vec)
{
    vector<vector<char>> ret = vec;
    for (size_t i = 0; i != vec.size(); i++)
    {
        for (size_t j = 0; j < i; j++)
        {
            swap(ret[i][j], ret[j][i]);
        }
    }
    return ret;
}

int main(int argc, int** argv)
{
    int num;
begin:
    cout << "Please input an integer from 1 to 5: ";
    cin >> num;
    if (num < 1 || num>5)
    {
        cerr << "Illegal input!" << endl;
        goto begin; // go to the label begin
    }
    vector<vector<char>> Mat = Mat_define(num);
    vector<vector<char>> T_Mat = transpose(Mat);
    vector<vector<int>> diff;
    int minimum = 0;
    for (size_t i = 0; i != num; i++)
    {
        vector<int> row;
        for (size_t j = 0; j != num; j++)
        {
            int difference = static_cast<int>(T_Mat[i][j]) - static_cast<int>(Mat[i][j]);
            row.push_back(difference);
            minimum = min(minimum, difference);
        }
        diff.push_back(row);
    }
    cout << "\nThe matrix is" << endl;
    cout << "┌" << setw(3 * num + 2) << "┐" << std::endl;
    for (auto& c1 : diff)
    {
        cout << "│";
        for (auto& c2 : c1)
        {
            c2 -= minimum;
            cout << setw(3) << c2;
        }
        cout << "│" << std::endl;
    }
    cout << "└" << setw(3 * num + 2) << "┘" << std::endl;

    // draw the life time chart
    cout << "\nThe life time chart:" << endl;
    cout << " T ";
    for (char ch = 'a'; ch - 'a' != num * num; ch++)
    {
        cout << " " << ch;
    }
    cout << " Number" << endl;
    int max_alr = 0;
    for (size_t t = 0;; t++)
    {
        cout << setw(2) << t << " ";
        int alr = num * num;
        for (size_t i = 0; i != num; i++)
        {
            for (size_t j = 0; j != num; j++)
            {
                if (num * i + j == t)
                {
                    if (diff[i][j] != 0) cout << " ┬";
                    else
                    {
                        cout << " ─";
                        alr--;
                    }
                }
                else if (num * i + j < t && t < num * i + j + diff[i][j]) cout << " ┼";
                else if (t == num * i + j + diff[i][j])
                {
                    cout << " ┴";
                    alr--;
                }
                else
                {
                    cout << " ─";
                    alr--;
                }
            }
        }
        max_alr = max(max_alr, alr);
        if (alr == max_alr) cout << "   " << alr << endl;
        else cout << "   " << alr << "+" << max_alr - alr << "=" << max_alr << endl;
        if (alr == 0 && t != 0) break;
    }
    cout << "\nThe answer is " << max_alr << "." << endl;
    cout << "\n-------------------------------------------------" << endl;
    cout << "This is a C++ program designed by Teddy van Jerry." << endl;
    return 0;
}

// ALL RIGHTS RESERVED (C) 2020 Teddy van Jerry

        \end{lstlisting}

        \emph{Tip: you can change the input to view the result of
        matrices of different orders.}

    \section{Allocation table}

        \ \ \ \ With the life time chart, it is known that
        the number of registers should not be smaller than 9.
        
        From the allocation chart, we know that 9 registers are enough
        as they all fit in the table.
        Thus 9 is the minimum number of registers.

    \section{Conclusion}

        \ \ \ \ The answer is 9 registers.

        \newpage

        \begin{figure}[htbp]
            
            %\subfigure[The matrix and the life time chart]
            %{
                \begin{minipage}{6 cm}
                    \centering
                    \includegraphics[height = 300 pt]{output.jpg}                   
                    \caption{\centering The matrix and the life time chart}
                \end{minipage}
            %}
            %\subfigure[The allocation table]
            %{
                \begin{minipage}{10 cm}
                    \centering
                    \includegraphics[height = 300 pt]{allocation.pdf}
                    \caption{The allocation table}
                \end{minipage}
            %}
            \label{result}
        \end{figure}

    \section*{Appendix}

    \ \ \ \ This passage is compiled by \LaTeX.\\

    The Cpp code is written by me using \emph{Visual Studio 2019}.\\

    You can get my \LaTeX \ code, C++ code and jpg pictures in my CSDN blog:

    Code:\hspace{41.6pt}
    \url{https://blog.csdn.net/weixin_50012998/article/details/109729764}

    C++ Program:
    \url{https://blog.csdn.net/weixin_50012998/article/details/109710091}

    Figure 1 :\hspace{24.2pt}
    \url{https://blog.csdn.net/weixin_50012998/article/details/109710091}
    
    Figure 2 :\hspace{24.2pt}
    \url{https://blog.csdn.net/weixin_50012998/article/details/109710222}

\bibliographystyle{ieeetr} % setting the cite style
\bibliography{ref}

\end{document}

\end{article}

界面

界面

效果

1
2
3
4

分析

为什么叫斗智斗勇呢?

  1. 起初插入232-249行的时候是可以的,但是发现引用的地方又都变成了问好。重新编译Bibtex,不行了。删掉这段再运行,Bibtex行了,结果pdfLaTeX又不行了。一会是我加的C++代码LaTeX误判语法错误,一会是说\usepackage{\usepackage{subfigure}}中重定义了,捣鼓好久之后后来又好莫名其妙好了。
  2. 然而同样的代码,一会两张图并排,一会又不在一排,而且注释掉的内容还删不得,一删格式又错了!
  3. 总结就是,要及时把合适的文件存下来!!!
  4. 还有就是到关键段落每写一句运行一下,这样就更容易知道你错在哪里了。

ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。


See also

Teddy van Jerry 的导航页

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值