$$ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Self-defined math definitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Math symbol commands \newcommand{\intd}{\,{\rm d}} % Symbol 'd' used in integration, such as 'dx' \newcommand{\diff}{{\rm d}} % Symbol 'd' used in differentiation \newcommand{\Diff}{{\rm D}} % Symbol 'D' used in differentiation \newcommand{\pdiff}{\partial} % Partial derivative \newcommand{DD}[2]{\frac{\diff}{\diff #2}\left( #1 \right)} \newcommand{Dd}[2]{\frac{\diff #1}{\diff #2}} \newcommand{PD}[2]{\frac{\pdiff}{\pdiff #2}\left( #1 \right)} \newcommand{Pd}[2]{\frac{\pdiff #1}{\pdiff #2}} \newcommand{\rme}{{\rm e}} % Exponential e \newcommand{\rmi}{{\rm i}} % Imaginary unit i \newcommand{\rmj}{{\rm j}} % Imaginary unit j \newcommand{\vect}[1]{\boldsymbol{#1}} % Vector typeset in bold and italic \newcommand{\phs}[1]{\dot{#1}} % Scalar phasor \newcommand{\phsvect}[1]{\boldsymbol{\dot{#1}}} % Vector phasor \newcommand{\normvect}{\vect{n}} % Normal vector: n \newcommand{\dform}[1]{\overset{\rightharpoonup}{\boldsymbol{#1}}} % Vector for differential form \newcommand{\cochain}[1]{\overset{\rightharpoonup}{#1}} % Vector for cochain \newcommand{\bigabs}[1]{\bigg\lvert#1\bigg\rvert} % Absolute value (single big vertical bar) \newcommand{\Abs}[1]{\big\lvert#1\big\rvert} % Absolute value (single big vertical bar) \newcommand{\abs}[1]{\lvert#1\rvert} % Absolute value (single vertical bar) \newcommand{\bignorm}[1]{\bigg\lVert#1\bigg\rVert} % Norm (double big vertical bar) \newcommand{\Norm}[1]{\big\lVert#1\big\rVert} % Norm (double big vertical bar) \newcommand{\norm}[1]{\lVert#1\rVert} % Norm (double vertical bar) \newcommand{\ouset}[3]{\overset{#3}{\underset{#2}{#1}}} % over and under set % Super/subscript for column index of a matrix, which is used in tensor analysis. \newcommand{\cscript}[1]{\;\; #1} % Star symbol used as prefix in front of a paragraph with no indent \newcommand{\prefstar}{\noindent$\ast$ } % Big vertical line restricting the function. % Example: $u(x)\restrict_{\Omega_0}$ \newcommand{\restrict}{\big\vert} % Math operators which are typeset in Roman font \DeclareMathOperator{\sgn}{sgn} % Sign function \DeclareMathOperator{\erf}{erf} % Error function \DeclareMathOperator{\Bd}{Bd} % Boundary of a set, used in topology \DeclareMathOperator{\Int}{Int} % Interior of a set, used in topology \DeclareMathOperator{\rank}{rank} % Rank of a matrix \DeclareMathOperator{\divergence}{div} % Curl \DeclareMathOperator{\curl}{curl} % Curl \DeclareMathOperator{\grad}{grad} % Gradient \DeclareMathOperator{\tr}{tr} % Trace \DeclareMathOperator{\span}{span} % Span $$

止于至善

As regards numerical analysis and mathematical electromagnetism

Principles about returning a large data chunk from a function

When a large data chunk is to be generated by a function, it can be either acquired from the function’s return value or passed by reference or pointer as an in/out variable. According to my development experience, I have summarized the following principles for choosing the appropriate solution.

  • Even though, there may be compiler optimization when a large object is used as the function’s return value, it is still not recommended. Instead, the large object should be passed to the function as a mutable reference, which can hold the returned data.

  • For the sake of improving the overall efficiency by limiting the number of times of memory allocation and releasing, the memory for the returned object should be allocated outside the working function, if its size can be predetermined. In this way, if the memory of this object can be reused, only one time of memory allocation and releasing is needed.

  • For the sake of user convenience, like matrix and vector operations in Octave, it is obvious that the memory allocation is performed inside the working function.

  • A practical design of the numerical library depends on whether it aims to achieve low level operations with high performance or realize a high level user friendly interface. The former prefers memory allocation outside the working function, while the latter inside.

  • LAPACK is a typical example of low level numerical library, where the memory for all data and work spaces is predefined and allocated.

posted @ 2021-09-29 06:55  皮波迪博士  阅读(56)  评论(0编辑  收藏  举报