473,890 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ - STL usage 'Advice'


I'm perusing a slide with roughly 12 bullets spread across 3 pages.
Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more
specifically the bullet that states:
" Avoid the STL unless absolutely necessary "

Now, I'm not acclimated with much C++ history, but something tells me
this is akin to trepidation that surrounded C++ during it's inception?
IOW, is this 'old school' rhetoric or ..... How do you refute this
argument?

I persued the web and there was discussion about code bloat (pre 2000
posts) but I'm not quite fure if I'm following that. Oh well, I
thought I'd inquire within. I'm a fan of lang.c++/learn and moderated
c++ and rest assured the vast majority of folks on here know what the
hell they're talking about so somebody will set me straight.

Granted, my background is signal proceesing, I've enjoyed the language
to include the STL and all it's glory (the Big O notation still
confuses me) but I'm tempted to go to the upcoming Vegas conference on
the language and ask Bjarne, Scott, Herb etc. this question :)
// formerly ma******@pegasu s.cc.ucf.edu

Oct 11 '05
11 4770
ma******@gmail. com wrote:
|| I think the C++ versions are much more easy to understand -- after
all you
|| can see the complex arithmetic. Also, I would be surprised if they
were
|| actually slower than the C version. After all this is a pretty
straight
|| forward translation.

Impressive. I suspect it's time to reivew std::complex and iterators
in depth. I ended up perusing those chapters (oso lightly) in
Josuttis. Do you have anything like this for FFT and IFFT ;) Makes me
think my custom versions (at the lab right now) are also unnatural.


I don't have the faintest idea about FFT or IFFT -- I do not even know what
IFFT means. However, if you post some code, I will have a look.
Best

Kai-Uwe Bux

Oct 12 '05 #11
ma******@gmail. com wrote:
Do you have anything like this for FFT and IFFT ;)

So, I read up a little bit on this FFT thing. Seems that it is an easy
divide and conquer idea -- at least for powers of 2 (I did not bother
thinking hard about other primes). So, here is a first draft of how one
could go about this.

// FFT
// ===
/*
| Discrete Fourier Transform can be regarded as evaluating a
| polynomial of degree N-1 on the powers
| omega^0, omega, omega^2, ..., omega^(N-1) where omega is a the
| Nth root of unity.
|
| Given a polynomial of even degree
|
| p(t) = a_0 + a_1 t + a_2 t^2 + ...
|
| we find:
|
| p(t) = a_0 + a_2 t^2 + a_4 t^4 + ... + t( a_1 + a_3 t^2 + ... )
| = q_e( t^2 ) + t q_o ( t^2 )
|
| where q_e and q_o are polynomials formed with the even and odd
| coefficients of p(t). Thus, we get
|
| p(1) = q_e(1) + omega^0 q_o(1)
| p(omega) = q_e(omega^2) + omega^1 q_o(omega^2)
| p(omega^2) = q_e(omega^4) + omega^2 q_o(omega^4)
| ...
|
| Note how on the rhs the Fouriertransfor ms of q_e and q_o appear. Thus:
*/

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <algorithm>
#include <complex>
#include <iterator>

namespace fft {

typedef double real_number;
typedef std::complex< real_number > complex_number;
typedef std::vector< complex_number > complex_vector;
typedef complex_vector: :size_type size_type;

complex_number unit_complex ( real_number arg ) {
return complex_number ( std::cos( arg ), std::sin( arg ) );
}

real_number rational_arg ( size_type i, size_type m ) {
return( 2.0 * 3.141592654 * static_cast<rea l_number>(i)
/ static_cast<rea l_number>(m) );
}

bool is_odd ( size_type m ) {
return( m % 2 != 0 );
}

class DFT_Pseudoitera tor
: public std::iterator< std::input_iter ator_tag,
complex_number,
std::ptrdiff_t,
complex_number const *,
complex_number const &> {

complex_number omega_to_the_i;
complex_number omega_to_the_ik ;
size_type k;

public:

DFT_Pseudoitera tor ( real_number sign,
size_type i,
size_type length,
bool past_end = false )
: omega_to_the_i ( unit_complex ( - sign * rational_arg( i,
length ) ) )
, omega_to_the_ik ( 1.0 )
, k ( past_end ? length : 0 )
{}

complex_number operator* ( void ) const {
return ( omega_to_the_ik );
}

DFT_Pseudoitera tor & operator++ ( void ) {
omega_to_the_ik *= omega_to_the_i;
++ k;
return( *this );
}

DFT_Pseudoitera tor operator++ ( int ) {
DFT_Pseudoitera tor dummy ( *this );
++ k;
return( dummy );
}

bool operator== ( DFT_Pseudoitera tor const & other ) const {
return( ( this->omega_to_the _i == other.omega_to_ the_i )
&&
( this->k == other.k ) );
}

bool operator!= ( DFT_Pseudoitera tor const & other ) const {
return( ! ( *this == other ) );
}

}; // DFT_Pseudoitera tor

void DFT( bool do_forward, complex_vector & z ) {
const real_number sign ( do_forward ? 1.0 : -1.0 );
const size_type length = z.size();

// allocate result
complex_vector result;
result.reserve( length );

// do matrix multiplication
for ( size_type i = 0; i < length; ++ i ) {
DFT_Pseudoitera tor coefficient_ite r ( sign, i, length );
result.push_bac k( std::inner_prod uct( z.begin(), z.end(),
coefficient_ite r,
complex_number( 0.0) ) );
}

// rescale if forward
if ( do_forward ) {
for ( size_type i = 0; i < length; ++ i ) {
result[i] /= static_cast<rea l_number>( length );
}
}

// copy data back (swap is fast!)
z.swap( result );
}

void FFT ( complex_vector & z ) {
size_type const length = z.size();
if ( is_odd( length ) ) {
// too bad, were odd
DFT( true, z );
} else {
// good, we are even.
// Let's divide and conquer!
complex_vector odd;
complex_vector even;
odd.reserve( length / 2 );
even.reserve( length / 2 );
for ( complex_vector: :const_iterator run = z.begin();
run != z.end(); ++ run ) {
even.push_back( *run );
++ run;
odd.push_back( *run );
}
FFT( even );
FFT( odd );
DFT_Pseudoitera tor omega_iter ( 1.0, 1, length );
for ( size_type i = 0; i < length / 2; ++ i, ++ omega_iter ) {
z[i] = 0.5 * ( even[i] + *omega_iter * odd[i] );
// the next line works because omega^(length/2) = -1 !!!
z[i + length/2] = 0.5 * ( even[i] - *omega_iter * odd[i] );
}
}
}

void IFFT ( complex_vector & z ) {
size_type const length = z.size();
if ( is_odd( length ) ) {
// too bad, were odd
DFT( false, z );
} else {
// good, we are even.
// Let's divide and conquer!
complex_vector odd;
complex_vector even;
odd.reserve( length / 2 );
even.reserve( length / 2 );
for ( complex_vector: :const_iterator run = z.begin();
run != z.end(); ++ run ) {
even.push_back( *run );
++ run;
odd.push_back( *run );
}
IFFT( even );
IFFT( odd );
DFT_Pseudoitera tor omega_iter ( -1.0, 1, length );
for ( size_type i = 0; i < length / 2; ++ i, ++ omega_iter ) {
z[i] = even[i] + *omega_iter * odd[i];
// the next line works because omega^(length/2) = -1 !!!
z[i + length/2] = even[i] - *omega_iter * odd[i];
}
}
}

template < typename ComplexIterator >
void FFT ( bool do_forward,
ComplexIterator const & from, ComplexIterator const & to ) {
complex_vector z ( from, to );
if ( do_forward ) {
FFT( z );
} else {
IFFT( z );
}
std::copy( z.begin(), z.end(), from );
}

} // namespace fft

int main ( void ) {
fft::complex_ve ctor z;
z.push_back( fft::complex_nu mber( 0.5, 2 ) );
z.push_back( fft::complex_nu mber( 0.7, 1.4 ) );
z.push_back( fft::complex_nu mber( 0.9, -2.1 ) );
z.push_back( fft::complex_nu mber( 0.9, -2.1 ) );
z.push_back( fft::complex_nu mber( -1.2, 1.1 ) );
z.push_back( fft::complex_nu mber( -1.2, 1.1 ) );

fft::DFT( true, z );
std::copy( z.begin(), z.end(),
std::ostream_it erator< fft::complex_nu mber >( std::cout, "\n" ) );
fft::DFT( false, z );
std::copy( z.begin(), z.end(),
std::ostream_it erator< fft::complex_nu mber >( std::cout, "\n" ) );
fft::FFT( true, z.begin(), z.end() );
std::copy( z.begin(), z.end(),
std::ostream_it erator< fft::complex_nu mber >( std::cout, "\n" ) );
fft::FFT( false, z.begin(), z.end() );
std::copy( z.begin(), z.end(),
std::ostream_it erator< fft::complex_nu mber >( std::cout, "\n" ) );
}

I did not bother putting in many comments. So, I cannot claim that I have
proof that this code is correct -- but I am reasonably confident, except
that I may have switched FFT and IFFT.

Also, please note that this code is in no way optimized for performance
(except for a slight change in DFT_Pseudoitera tor that now avoids
unnecessary calls to trig-functions). I am pretty sure that one can
eliminate many of the copies that are made just to separate the even and
odd indices. I think, that one should be able to do most of this in place
(maybe some fancy slice tricks from the <valarray> header would be cool).
But any of that would probably make the code more difficult to understand.

Also, a final version should probably be completely templated on the
real_number type.
Best

Kai-Uwe Bux
Oct 12 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5490
by: Fabian Knopf | last post by:
Hi @ll, my IBM DB2 server runs with 60% Cpu Usage but no task has to be done. Why this ??? Cu, Fabian
8
21921
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At that time, I get a SocketException with the message "Only one usage of each socket address (protocol/network address/port) is normally permitted". This happen if you use localhost or between two distinct computers. And, for a period of time after the...
7
5008
by: LBT | last post by:
I have a window service written using VB.NET. This window service will scan folders for file and grab the file content to be inserted to SQL Server on file detection. There are altogether 18 folders to be watched. Each folder is assigned with a timer for watching purpose. Hence there will be 18 timers and each timer is set to elapse on every second. Problem here, once the window service is installed and started, the CPU usage is very...
2
1633
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the main problem is that the mem usage continues to grow until the Service stops responding. I have received advice to: "create those objects at a class level; instantiate them when the service starts, and dispose of them when the service ends. Then...
1
2009
by: Larry Neylon | last post by:
Hi, I'm working on a VBScript application on IIS6 and I'm looking for some advice about the best way of replacing or improving session variable usage. The application is in a secure extranet environment. Currently the application has a search customers page with 10 search fields which list the results below the search fields. The requirement for this screen was that the user could return to this result page at any point from any page...
4
1683
by: Mark | last post by:
I want to create a collection class that will be strongly typed (store a specific object type), be keyed with a case insensitive string, and be able to access objects stored by index, or sequentially (in the order stored) via "For Each". I know I could code this from scratch - or derived from a number of framework classes, but I'm not sure of the pros/cons of various possibilities. I'd like to use some of the new Generics - they are...
6
2859
by: John | last post by:
I want to understand the usage of _SP in turbo C. Is _SP the stack pointer register of the CPU? That means _SP should point to the top of the procedure call stack when we make function calls? I assume _SP is only useful when making function calls here. Here's the program I wrote to test out how _SP behaves, but the result is main sp=-10 func1 sp=-14
0
3094
by: greg.novak | last post by:
I am using Python to process particle data from a physics simulation. There are about 15 MB of data associated with each simulation, but there are many simulations. I read the data from each simulation into Numpy arrays and do a simple calculation on them that involves a few eigenvalues of small matricies and quite a number of temporary arrays. I had assumed that that generating lots of temporary arrays would make my program run slowly,...
1
1697
by: Julian Moorhouse | last post by:
Hi, I'm trying to analyse my vb.net program which is going to do some processing on a web server. I'm using vb.net 1.1 My ISP says he will kill my program if it uses 100% cpu usage for a more than a few seconds. I've tried the CLR Profiler and another free profiler, but none show
0
9815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8008
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.