epix-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ePiX-users] integer to string conversions


From: Andrew D. Hwang
Subject: Re: [ePiX-users] integer to string conversions
Date: Mon, 18 Dec 2006 14:01:04 -0500 (EST)

On Sun, 17 Dec 2006, Gunnar wrote:

I would find it very helpful if there were an easy built-in way of converting numbers to strings in epix. Placing a lot of labels is preferly done by using a for-loop and

for (x=0;x<10;x++)
   label(P(x,f(x)),"$(x,f(x)$");

doesn't give me the desired labels like (2,f(2))   (3,f(3))  etc.
Any suggestions on this? Could there perhaps be an overloaded function
std::string n2s(double x)
std::string n2s(int x)

to convert Number to String?

Dear Gunnar,

Number-to-string conversion can be accomplished by reading into a stringstream and writing out a string (example below). However, for labeling you'd probably want to provide an x value and a function, and write an output string containing commas, parentheses, etc.

Offhand, it seems a general-purpose function might be difficult to write, but if you have specific label formats you use frequently (e.g., labeled pairs at specified locations, as in your example), it should be easy to hack something, such as:

#include <sstream>
void labels(double f(double), const std::string& fcn,
  double tmin, double tmax, int n, const P& offset, epix_label_posn align)
{
  const double dt((tmax-tmin)/n); // step size

  for (int i=0; i<=n; ++i)
    {
      double t(tmin + i*dt); // function evaluation point
      std::ostringstream obuf; // write string text here
      obuf << "\\bigl(" << t << "," << fcn << "(" << t << ")\\bigr)";

      label(P(t, f(t)), offset, obuf.str(), align); // write the label
    }
}

A "typical" usage might look like:

   pair_labels(exp, "\\exp", x_min, x_max, 4, P(0,-4), br);

Is that sort of what you had in mind?

Best,
Andy

Andrew D. Hwang                 address@hidden
Department of Math and CS       http://mathcs.holycross.edu/~ahwang
College of the Holy Cross       (508) 793-2458 (Office: 320 Swords)
Worcester, MA, 01610-2395       (508) 793-3530 (fax)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]