CPP008_Templates, EXceptions and Files

本文深入探讨了C++中的模板和异常处理技术,包括函数模板、类模板、模板特化及异常抛出和捕获等内容,展示了如何利用这些特性增强代码的泛用性和安全性。

Function Templates

Functions and classes help to make programs easier to write safer and more maintainable. However, while functions and classes do have all of those advantages, in certain cases they can also be somewhat limited by C++'s requirement that you specify types for all of your parameters.

For example, you might want to write a function that calculates the sum of two numbers, similar to this:

int sum(int a,int b){
	return a+b;	
}

You can use templates to define functions as well as classes. Let’s see how they work.

We can now call the function for two integers in our main.

#include <iostream>
using namespace std;

int sum(int a, int b) {
    return a+b;
}

int main () {
    int x=7, y=15;
    cout << sum(x, y) << endl;
}

The function works as expected, but is limited solely to integers.

It becomes necessary to write a new function for each new type, such as doubles.

double sum(double a, double b) {
  return a+b;
}

Wouldn’t it be much more efficient to be able to write one version of sum() to work with parameters of any type?
Function templates give us the ability to do that!
With function templates, the basic idea is to avoid the necessity of specifying an exact type for each variable. Instead, C++ provides us with the capability of defining functions using placeholder types, called template type parameters.

To define a function template, use the keyword template, followed by the template type definition:

template <class T>

We named our template type T, which is a generic data type.

Now we can use our generic data type T in the function:

#include<iostream>;
using namespace std;

//int sum(int a, int b){
//	return a + b;
//}

template <class T>
T sum(T a, T b){
	return a + b;
}

int main(){
	int x = 7, y = 15;
	cout << sum(x, y)<<endl;

	double j = 7.1, k = 8.2;
	cout << sum(j, k) << endl;
	system("PAUSE");
}

The function returns a value of the generic type T, taking two parameters, also of type T.

Our new function worked exactly as the previous one for integer values did.

The compiler automaticallly calls the function for the corresponding type.

When creating a template type parameter, the keyword typename may be used as an alternative to the keyword class: template <typename T>.
In this context, the keywords are identical, but throughout this course, we’ll use the keyword class.

Template functions can save a lot of time, because they are written only once, and work with different types.
Template functions reduce code maintenance, because duplicate code is reduced significantly.

Enhanced safety is another advantage in using template functions, since it’s not necessary to manually copy functions and change types.

Function Templates with Multiple Parameters

Function templates also make it possible to work with multiple generic data types. Define the data types using a comma-separated list.
Let’s create a function that compares arguments of varying data types (an int and a double), and prints the smaller one.
template <class T, class U>

As you can see, this template declares two different generic data types, T and U.

Now we can continue with our function declaration:

template<class T, class U>
T smaller(T a, U n){
	return(a<b ? a:b);
}

The ternary operation checks the a<b condition and returns the corresponding result. The expression (a<b ? a:b) is equivalent to the expression if a is smaller than b, return a, else return b.

The whole codes:

#include <iostream>
using namespace std;

template <class T, class U>
T smaller(T a, U b) {
    return (a < b ? a : b);
}

int main () {
    int x=72;
    double y=15.34;
    cout << smaller(x, y) << endl;
    system("PAUSE");
}
// Outputs 15

The output converts to an integer, because we specified the function template’s return type to be of the same type as the first parameter (T), which is an integer.

T is short for Type, and is a widely used name for type parameters.
It’s not necessary to use T, however; you can declare your type parameters using any identifiers that work for you. The only terms you need to avoid are C++ keywords.

Remember that when you declare a template parameter, you absolutely must use it in your function definition. Otherwise, the compiler will complain!

Class Templates

Just as we can define function templates, we can also define class templates, allowing classes to have members that use template parameters as type.
The same syntax is used to define the class template:

tempalte <class T>
class Myclass{
}

Just as with function templates, you can define more than one generic data type by using comma-separated list.

As an example, let’s create a class Pair, that will be holding a pair of values of a generic type.

template <class T>
class Pair {
 private:
  T first, second;
 public:
  Pair (T a, T b):
   first(a), second(b) {
  }
};

The code above declares a class template Pair, with two private variables of a generic type, and one constructor to initialize the variables.

A specific syntax is required in case you define your member functions outside of your class - for example in a separate source file.
You need to specify the generic type in angle brackets after the class name.
For example, to have a member function bigger() defined outside of the class, the following syntax is used:

template <class T>
class Pair {
 private:
  T first, second;
 public:
  Pair (T a, T b):
   first(a), second(b){
  }
  T bigger();
};

template <class T>
T Pair<T>::bigger() {
  // some code
}

A specific syntax is required in case you define your member functions outside of your class.

The bigger function returns the greater value of the two member variables.

template <class T>
class Pair {
 private:
  T first, second;
 public:
  Pair (T a, T b):
   first(a), second(b){
  }
  T bigger();
};

template <class T>
T Pair<T>::bigger() {
  return (first>second ? first : second);
}

The ternery operator compares the two variables, returning the greater one.

To create objects of the template class for different types, specify the data type in angle brackets, as we did when defining the function outside of the class.
Here, we create a Pair object for integers.

#include<iostream>
using namespace std;

template <class T>
class Pair{
private:
	T first, second;
public:
	Pair(T a, T b) :
		first(a), second(b){}
	T bigger();
};

template <class T>
T Pair<T>::bigger(){
	return (first > second ? first : second);
}

int main(){
	Pair <int> obj(11, 22);
	cout << obj.bigger()<<endl;
	system("PAUSE");
	return 0;
}

We can use the same class to create an object that stores any other type.

#include <iostream>
using namespace std;

template <class T>
class Pair {
    private:
        T first, second;
    public:
        Pair (T a, T b):
        first(a), second(b) { }
        T bigger();
};

template <class T>
T Pair<T>::bigger() {
    return (first>second ? first : second);
}

int main()
{
    Pair <double> obj(23.43, 5.68);
    cout << obj.bigger();
    
    system("PAUSE");
    return 0;
}
// Outputs 23.43

Template Specialization

In case of regular class templates, the way the class handles different data types is the same; the same code runs for all data types.
Template specialization allows for the definition of a different implementation of a template when a specific type is passed as a template argument.

For example, we might need to handle the character data type in a different manner than we do numeric data types.
To demonstrate how this works, we can first create a regular template.

template <class T>
class MyClass {
 public:
  MyClass (T x) {
   cout <<x<<" -  not a char"<<endl;
  }
};

As a regular class template, MyClass treats all of the various data types in the same way.

To specify different behavior for the data type char, we would create a template specialization.

template <class T>
class MyClass {
 public:
  MyClass (T x) {
   cout <<x<<" -  not a char"<<endl;
  }
};

template < >
class MyClass<char> {
 public:
  MyClass (char x) {
   cout <<x<<" is a char!"<<endl;
  }
};

First of all, notice that we precede the class name with template<>, including an empty parameter list. This is because all types are known and no template arguments are required for this specialization, but still, it is the specialization of a class template, and thus it requires to be noted as such.

But more important than this prefix, is the specialization parameter after the class template name. This specialization parameter itself identifies the type for which the template class is being specialized (char).

In the example above, the first class is the generic template, while the second is the specialization.
If necessary, your specialization can indicate a completely different behavior from the behavior of the generic template.

The next step is to declare objects of different types and check the result:

#include <iostream>
using namespace std;

template <class T>
class MyClass {
    public:
        MyClass (T x) {
            cout <<x<<" -  not a char"<<endl;
        }
};

template < >
class MyClass<char> {
    public:
        MyClass (char x) {
            cout <<x<<" is a char!"<<endl;
        }
};

int main () {
    MyClass<int> ob1(42);
    MyClass<double> ob2(5.47);
    MyClass<char> ob3('s');
    system("PAUSE");
    return 0;
}
/* Output: 
42 - not a char
5.47 - not a char
s is a char!
*/

As you can see, the generic template worked for int and double. However, our template specialization was invoked for the char data type.

Keep in mind that there is no member “inheritance” from the generic template to the specialization, so all members of the template class specializations must be defined on their own.

Exceptions

Problems that occur during program execution are called exceptions.
In C++ exceptions are responses to anomalies that arise while the program is running, such as an attempt to divide by zero.

Throwing Exceptions

C++ exception handling is built upon three keywords: try, catch and throw.
throw is used to throw an exception when a problem shows up.

int motherAge = 29;
int sonAge = 36;
if (sonAge > motherAge){
	throw "Wrong age values";
}

The code looks at sonAge and motherAge, and throws an exception if sonAge is found to be the greater of the two.

In the throw statement, the operand determines a type for the exception. This can be any expression. The type of the expression’s result will determine the type of the exception thrown.

Catching Exceptions

A try block identifies a block of code that will activate specific exceptions. It’s followed by one or more catch blocks. The catch keyword represents a block of code that executes when a particular exception is thrown.
Code that could generate an exception is surrounded with the try/catch block.
You can specify what type of exception you want to catch by the exception declaration that appears in parentheses following the keyword catch.
For example:

#include<iostream>
using namespace std;

int main()
{
	try {
		int motherAge = 29;
		int sonAge = 36;
		if (sonAge > motherAge){
			throw 99;
		}
	}
	catch (int x){
		cout << "Wrong age values - Error" << x<<endl;
	}
	system("PAUSE");

	return 0;
}

//Outputs "Wrong age values - Error 99"

The try block throws the exception, and the catch block then handles it.
The error code 99, which is an integer, appears in the throw statement, so it results in an exception of type int.

Multiple catch statements may be listed to handle the various exceptions in case multiple exceptions are thrown by the try block.

More on Exceptions

Exception handling is particularly useful when dealing with user input.
For example, for a program that requests user input of two numbers, and then outputs their division, be sure that you handle division by zero, in case your user enters 0 as the second number.

int main() {
  int num1;
  cout <<"Enter the first number:";
  cin >> num1;

  int num2;
  cout <<"Enter the second number:";
  cin >> num2;

  cout <<"Result:"<<num1 / num2;
}

This program works perfectly if the user enters any number besides 0.

In case of 0 the program crashes, so we need to handle that input.

Exception Handling

In the event that the second number is equal to 0, we need to throw an exception.

#include <iostream>
using namespace std;

int main() {
    int num1;
    cout <<"Enter the first number:";
    cin >> num1;
    
    int num2;
    cout <<"Enter the second number:";
    cin >> num2;
    
    if(num2 == 0) {
        throw 0;
    }
    
    cout <<"Result:"<<num1 / num2;  
}

This code throws an exception with the code 0 of type integer.

Next stop: Using the try/catch block to handle the exception!

Now we need to handle the thrown exception using a try/catch block.

#include <iostream>
using namespace std;

int main() {
    try {
        int num1;
        cout <<"Enter the first number:";
        cin >> num1;
        
        int num2;
        cout <<"Enter the second number:";
        cin >> num2;
        
        if(num2 == 0) {
            throw 0;
        }
        
        cout <<"Result:"<<num1 / num2; 
    }
    catch(int x) {
        cout <<"Division by zero!";
    }
}

This results in the output of “Division by zero!” as an alternative to a program crash, when 0 is entered as the second number.

In our case, we catch exceptions of type integer only. It’s possible to specify that your catch block handles any type of exception thrown in a try block. To accomplish this, add an ellipsis (…) between the parentheses of catch:

try {
  // code
} catch(...) {
  // code to handle exceptions
}

Working with Files

Another useful C++ feature is the ability to read and write to files. That requires the standard C++ library called fstream.
Three new data types are defined in fstream:
ofstream: Output file stream that creates and writes information to files.
ifstream: Input file stream that reads information from files.
fstream: General file stream, with both ofstream and ifstream capabilities that allow it to create, read, and write information to files.

To perform file processing in C++, header files <iostream> and <fstream> must be included in the C++ source file.

#include<iostream>
#include<fstream>

These classes are derived directly or indirectly from the classes istream and ostream. We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream.

Opening a File

A file must be opened before you can read from it or write to it.
Either the ofstream or fstream object may be used to open a file for writing.
Let’s open a file called “test.txt” and write some content to it:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
  ofstream MyFile;
  MyFile.open("test.txt");

  MyFile << "Some text. \n";
}

The above code creates an ofstream object called MyFile, and uses the open() function to open the “test.txt” file on the file system. As you can see, the same stream output operator is used to write into the file.

If the specified file does not exist, the open function will create it automatically.

When you’ve finished working with a file, close it using the member function close().

#include <iostream>
#include <fstream>
using namespace std;

int main() {
  ofstream MyFile;
  MyFile.open("test.txt");

  MyFile << "Some text! \n";
  MyFile.close();
}

Running this code will cause a “test.txt” file to be created in the directory of your project with “Some text!” written in it.

You also have the option of specifying a path for your file in the open function, since it can be in a location other than that of your project.

More on Files

You can also provide the path to your file using the ofstream objects constructor, instead of calling the open function.

#include <fstream>
using namespace std;

int main() {
  ofstream MyFile("test.txt");

  MyFile << "This is awesome! \n";
  MyFile.close();
}

As with the open function, you can provide a full path to your file located in a different directory.

Under certain circumstances, such as when you don’t have file permissions, the open function can fail.
The is_open() member function checks whether the file is open and ready to be accessed.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
  ofstream MyFile("test.txt");

  if (MyFile.is_open()) {
   MyFile << "This is awesome! \n";
  }
  else {
   cout << "Something went wrong";
  }
  MyFile.close();
}

The is_open() member function checks whether the file is open and ready to be accessed.

File Opening Modes

An optional second parameter of the open function defines the mode in which the file is opened. This list shows the supported modes.

在这里插入图片描述
All these flags can be combined using the bitwise operator OR (|).
For example, to open a file in write mode and truncate it, in case it already exists, use the following syntax:

ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );

Reading from a File
You can read information from a file using an ifstream or fstream object.

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    ofstream MyFile1("test.txt");
    
    MyFile1 << "This is awesome! \n";
    MyFile1.close();

    string line;
    ifstream MyFile("test.txt");
    while ( getline (MyFile, line) ) {
        cout << line << '\n';
    }
    MyFile.close();
}

The getline function reads characters from an input stream and places them into a string.

The example above reads a text file and prints the contents to the screen.
Our while loop uses the getline function to read the file line by line.

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting numpy==1.19.2 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bf/e8/15aea783ea72e2d4e51e3ec365e8dc4a1a32c9e5eb3a6d695b0d58e67cdd/numpy-1.19.2.zip (7.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.3/7.3 MB 25.1 MB/s 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [80 lines of output] Running from numpy source directory. setup.py:470: UserWarning: Unrecognized setuptools command, proceeding with generating Cython sources and expanding templates run_build = parse_setuppy_commands() performance hint: _common.pyx:275:19: Exception check after calling 'random_func' will always require the GIL to be acquired. Declare 'random_func' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:299:19: Exception check after calling 'random_func' will always require the GIL to be acquired. Declare 'random_func' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:322:50: Exception check after calling 'random_func' will always require the GIL to be acquired. Declare 'random_func' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:426:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:465:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:509:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:592:36: Exception check after calling 'f0' will always require the GIL to be acquired. Declare 'f0' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:596:36: Exception check after calling 'f1' will always require the GIL to be acquired. Declare 'f1' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:600:36: Exception check after calling 'f2' will always require the GIL to be acquired. Declare 'f2' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:604:36: Exception check after calling 'f3' will always require the GIL to be acquired. Declare 'f3' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:638:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:675:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:712:63: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:754:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:785:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:903:40: Exception check after calling 'f0' will always require the GIL to be acquired. Declare 'f0' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:907:40: Exception check after calling 'fd' will always require the GIL to be acquired. Declare 'fd' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:911:41: Exception check after calling 'fdd' will always require the GIL to be acquired. Declare 'fdd' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:916:40: Exception check after calling 'fi' will always require the GIL to be acquired. Declare 'fi' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:920:41: Exception check after calling 'fdi' will always require the GIL to be acquired. Declare 'fdi' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:924:38: Exception check after calling 'fiii' will always require the GIL to be acquired. Declare 'fiii' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:960:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:1002:32: Exception check after calling 'f1' will always require the GIL to be acquired. Declare 'f1' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. Error compiling Cython file: ------------------------------------------------------------ ... # Check preconditions on arguments mean = np.array(mean) cov = np.array(cov) if size is None: shape = [] elif isinstance(size, (int, long, np.integer)): ^ ------------------------------------------------------------ _generator.pyx:3470:36: undeclared name not builtin: long Processing numpy/random\_bounded_integers.pxd.in Processing numpy/random\bit_generator.pyx Processing numpy/random\mtrand.pyx Processing numpy/random\_bounded_integers.pyx.in Processing numpy/random\_common.pyx Processing numpy/random\_generator.pyx Traceback (most recent call last): File "C:\Users\renhongda\AppData\Local\Temp\pip-install-lkcf8c84\numpy_e74901428b4e4b6099715646dfb8bdde\tools\cythonize.py", line 235, in <module> main() File "C:\Users\renhongda\AppData\Local\Temp\pip-install-lkcf8c84\numpy_e74901428b4e4b6099715646dfb8bdde\tools\cythonize.py", line 231, in main find_process_files(root_dir) File "C:\Users\renhongda\AppData\Local\Temp\pip-install-lkcf8c84\numpy_e74901428b4e4b6099715646dfb8bdde\tools\cythonize.py", line 222, in find_process_files process(root_dir, fromfile, tofile, function, hash_db) File "C:\Users\renhongda\AppData\Local\Temp\pip-install-lkcf8c84\numpy_e74901428b4e4b6099715646dfb8bdde\tools\cythonize.py", line 188, in process processor_function(fromfile, tofile) File "C:\Users\renhongda\AppData\Local\Temp\pip-install-lkcf8c84\numpy_e74901428b4e4b6099715646dfb8bdde\tools\cythonize.py", line 77, in process_pyx subprocess.check_call( File "C:\Users\renhongda\.conda\envs\renbiubiu\lib\subprocess.py", line 373, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['C:\\Users\\renhongda\\myenv\\Scripts\\python.exe', '-m', 'cython', '-3', '--fast-fail', '-o', '_generator.c', '_generator.pyx']' returned non-zero exit status 1. Cythonizing sources Traceback (most recent call last): File "C:\Users\renhongda\myenv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() File "C:\Users\renhongda\myenv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "C:\Users\renhongda\myenv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 175, in prepare_metadata_for_build_wheel return hook(metadata_directory, config_settings) File "C:\Users\renhongda\AppData\Local\Temp\pip-build-env-549h2jd7\overlay\Lib\site-packages\setuptools\build_meta.py", line 157, in prepare_metadata_for_build_wheel self.run_setup() File "C:\Users\renhongda\AppData\Local\Temp\pip-build-env-549h2jd7\overlay\Lib\site-packages\setuptools\build_meta.py", line 248, in run_setup super(_BuildMetaLegacyBackend, File "C:\Users\renhongda\AppData\Local\Temp\pip-build-env-549h2jd7\overlay\Lib\site-packages\setuptools\build_meta.py", line 142, in run_setup exec(compile(code, __file__, 'exec'), locals()) File "setup.py", line 499, in <module> setup_package() File "setup.py", line 479, in setup_package generate_cython() File "setup.py", line 274, in generate_cython raise RuntimeError("Running cythonize failed!") RuntimeError: Running cythonize failed! [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> numpy note: This is an issue with the package mentioned above, not pip. hint: See above for details. (myenv) (renbiubiu) PS C:\Users\renhongda> pip install numpy==1.19.2 Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting numpy==1.19.2 Using cached https://pypi.tuna.tsinghua.edu.cn/packages/bf/e8/15aea783ea72e2d4e51e3ec365e8dc4a1a32c9e5eb3a6d695b0d58e67cdd/numpy-1.19.2.zip (7.3 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [80 lines of output] Running from numpy source directory. setup.py:470: UserWarning: Unrecognized setuptools command, proceeding with generating Cython sources and expanding templates run_build = parse_setuppy_commands() performance hint: _common.pyx:275:19: Exception check after calling 'random_func' will always require the GIL to be acquired. Declare 'random_func' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:299:19: Exception check after calling 'random_func' will always require the GIL to be acquired. Declare 'random_func' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:322:50: Exception check after calling 'random_func' will always require the GIL to be acquired. Declare 'random_func' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:426:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:465:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:509:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:592:36: Exception check after calling 'f0' will always require the GIL to be acquired. Declare 'f0' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:596:36: Exception check after calling 'f1' will always require the GIL to be acquired. Declare 'f1' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:600:36: Exception check after calling 'f2' will always require the GIL to be acquired. Declare 'f2' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:604:36: Exception check after calling 'f3' will always require the GIL to be acquired. Declare 'f3' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:638:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:675:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:712:63: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:754:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:785:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:903:40: Exception check after calling 'f0' will always require the GIL to be acquired. Declare 'f0' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:907:40: Exception check after calling 'fd' will always require the GIL to be acquired. Declare 'fd' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:911:41: Exception check after calling 'fdd' will always require the GIL to be acquired. Declare 'fdd' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:916:40: Exception check after calling 'fi' will always require the GIL to be acquired. Declare 'fi' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:920:41: Exception check after calling 'fdi' will always require the GIL to be acquired. Declare 'fdi' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:924:38: Exception check after calling 'fiii' will always require the GIL to be acquired. Declare 'fiii' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:960:31: Exception check after calling 'f' will always require the GIL to be acquired. Declare 'f' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. performance hint: _common.pyx:1002:32: Exception check after calling 'f1' will always require the GIL to be acquired. Declare 'f1' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. Error compiling Cython file: ------------------------------------------------------------ ... # Check preconditions on arguments mean = np.array(mean) cov = np.array(cov) if size is None: shape = [] elif isinstance(size, (int, long, np.integer)): ^ ------------------------------------------------------------ _generator.pyx:3470:36: undeclared name not builtin: long Processing numpy/random\_bounded_integers.pxd.in Processing numpy/random\bit_generator.pyx Processing numpy/random\mtrand.pyx Processing numpy/random\_bounded_integers.pyx.in Processing numpy/random\_common.pyx Processing numpy/random\_generator.pyx Traceback (most recent call last): File "C:\Users\renhongda\AppData\Local\Temp\pip-install-kft4ap1i\numpy_16037c0555ef4c15be9a8dda8fa94bc6\tools\cythonize.py", line 235, in <module> main() File "C:\Users\renhongda\AppData\Local\Temp\pip-install-kft4ap1i\numpy_16037c0555ef4c15be9a8dda8fa94bc6\tools\cythonize.py", line 231, in main find_process_files(root_dir) File "C:\Users\renhongda\AppData\Local\Temp\pip-install-kft4ap1i\numpy_16037c0555ef4c15be9a8dda8fa94bc6\tools\cythonize.py", line 222, in find_process_files process(root_dir, fromfile, tofile, function, hash_db) File "C:\Users\renhongda\AppData\Local\Temp\pip-install-kft4ap1i\numpy_16037c0555ef4c15be9a8dda8fa94bc6\tools\cythonize.py", line 188, in process processor_function(fromfile, tofile) File "C:\Users\renhongda\AppData\Local\Temp\pip-install-kft4ap1i\numpy_16037c0555ef4c15be9a8dda8fa94bc6\tools\cythonize.py", line 77, in process_pyx subprocess.check_call( File "C:\Users\renhongda\.conda\envs\renbiubiu\lib\subprocess.py", line 373, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['C:\\Users\\renhongda\\myenv\\Scripts\\python.exe', '-m', 'cython', '-3', '--fast-fail', '-o', '_generator.c', '_generator.pyx']' returned non-zero exit status 1. Cythonizing sources Traceback (most recent call last): File "C:\Users\renhongda\myenv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() File "C:\Users\renhongda\myenv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "C:\Users\renhongda\myenv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 175, in prepare_metadata_for_build_wheel return hook(metadata_directory, config_settings) File "C:\Users\renhongda\AppData\Local\Temp\pip-build-env-5zty90i8\overlay\Lib\site-packages\setuptools\build_meta.py", line 157, in prepare_metadata_for_build_wheel self.run_setup() File "C:\Users\renhongda\AppData\Local\Temp\pip-build-env-5zty90i8\overlay\Lib\site-packages\setuptools\build_meta.py", line 248, in run_setup super(_BuildMetaLegacyBackend, File "C:\Users\renhongda\AppData\Local\Temp\pip-build-env-5zty90i8\overlay\Lib\site-packages\setuptools\build_meta.py", line 142, in run_setup exec(compile(code, __file__, 'exec'), locals()) File "setup.py", line 499, in <module> setup_package() File "setup.py", line 479, in setup_package generate_cython() File "setup.py", line 274, in generate_cython raise RuntimeError("Running cythonize failed!") RuntimeError: Running cythonize failed! [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> numpy note: This is an issue with the package mentioned above, not pip. hint: See above for details.
11-19
数据集介绍:电力线目标检测数据集 一、基础信息 数据集名称:电力线目标检测数据集 图片数量: 训练集:2898张图片 验证集:263张图片 测试集:138张图片 总计:3299张图片 分类类别: 类别ID: 0(电力线) 标注格式: YOLO格式,包含对象标注信息,适用于目标检测任务。 数据格式:JPEG/PNG图片,来源于空中拍摄或监控视觉。 二、适用场景 电力设施监控与巡检: 数据集支持目标检测任务,帮助构建能够自动识别和定位电力线的AI模型,用于无人机或固定摄像头巡检,提升电力设施维护效率和安全性。 能源与公用事业管理: 集成至能源管理系统中,提供实时电力线检测功能,辅助进行风险 assessment 和预防性维护,优化能源分配。 计算机视觉算法研究: 支持目标检测技术在特定领域的应用研究,促进AI在能源和公用事业行业的创新与发展。 专业培训与教育: 数据集可用于电力行业培训课程,作为工程师和技术人员学习电力线检测与识别的重要资源。 三、数据集优势 标注精准可靠: 每张图片均经过专业标注,确保电力线对象的定位准确,适用于高精度模型训练。 数据多样性丰富: 包含多种环境下的电力线图片,如空中视角,覆盖不同场景条件,提升模型的泛化能力和鲁棒性。 任务适配性强: 标注格式兼容YOLO等主流深度学习框架,便于快速集成和模型开发,支持目标检测任务的直接应用。 实用价值突出: 专注于电力线检测,为智能电网、自动化巡检和能源设施监控提供关键数据支撑,具有较高的行业应用价值。
【弹簧阻尼器】基于卡尔曼滤波弹簧质量阻尼器系统噪声测量实时状态估计研究(Matlab代码实现)内容概要:本文围绕“基于卡尔曼滤波的弹簧质量阻尼器系统噪声测量与实时状态估计”展开研究,利用Matlab代码实现对系统状态的精确估计。重点在于应用卡尔曼滤波技术处理系统中存在的噪声干扰,提升对弹簧质量阻尼器系统动态行为的实时观测能力。文中详细阐述了系统建模、噪声特性分析及卡尔曼滤波算法的设计与实现过程,展示了滤波算法在抑制测量噪声、提高状态估计精度方面的有效性。同时,该研究属于更广泛的信号处理与状态估计技术应用范畴,适用于复杂动态系统的监控与控制。; 适合人群:具备一定控制系统理论基础和Matlab编程经验的高校研究生、科研人员及工程技术人员,尤其适合从事动态系统建模、状态估计与滤波算法研究的相关人员。; 使用场景及目标:①应用于机械、航空航天、自动化等领域中对振动系统状态的高精度实时估计;②为噪声环境下的传感器数据融合与状态预测提供算法支持;③作为卡尔曼滤波算法在实际物理系统中应用的教学与科研案例。; 阅读建议:建议读者结合Matlab代码实践,深入理解系统建模与滤波器设计的关键步骤,关注噪声建模与滤波参数调优对估计性能的影响,并可进一步拓展至扩展卡尔曼滤波(EKF)或无迹卡尔曼滤波(UKF)在非线性系统中的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值