Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:50:25+00:00 2026-06-07T02:50:25+00:00

Consider the code below: public class Analyzer { protected Func f,fd; public delegate double

  • 0

Consider the code below:

public class Analyzer {
        protected Func f,fd;
        public delegate double Func( double x );
        public Analyzer( Func f, Func fd ) {
            this.f = f;
            this.fd = fd;
        }
        public Analyzer( Func f ) {
            this.f = f;
            fd = dx;
        }
        public Analyzer( ) { }
        protected double dx( double x ) {
            double h = x / 50.0;
            return ((f(x + h) - f(x - h)) / (2 * h)); 
        }
        public double evaluate(double x) {
            return f( x );
        }
        public double evaluateDerived( double x ) {
            return fd( x );
        }
        public double solve(double x0) {
            double eps = 1, x1 = f(x0), x2 = fd(x0);
            do x0 = x0 - ( f( x0 ) / fd( x0 ) );
            while ( f( x0 ) > eps );
            return x0;
        }
    }
    public class PolyAnalyzer : Analyzer {
        private double[] coefs;
        public PolyAnalyzer( params double[] coef ) {
            coefs = coef;
            f = poly;
            fd = dx;
        }
        private double poly( double x ) {
            double sum = 0;
            for ( int i = 0 ; i < coefs.Length ; i++ ) {
                sum += coefs[i] * Math.Pow(x,coefs.Length-1-i);
            }
            return sum;
        }
    }

I was trying to think of a way to send poly to the constructor Analyser(Func f), is there a way to do that here? tried something like :

public PolyAnalyzer( params double[] coef ) : base(new Func(poly)){
            coefs = coef;
        }

but it doesnt compile… compilation error::
An object reference is required for the nonstatic field, method, or property ‘member’

Id appriciate a well explained answer, and not just how its done… 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:50:26+00:00Added an answer on June 7, 2026 at 2:50 am

    In my opinion you’re trying to combine object-oriented inheritance and functional programming, and it’s not working well in this case.

    I would write

    public abstract class Analyzer {
        protected abstract double Fd(double x);
        // ...
    }
    

    and override it in the descendant class (if you want to stick with an OO hierarchy). This is classic Strategy Pattern implementation.

    To address your comment, if you want Analyzer to be instantiable, use:

    public class Analyzer {
        protected virtual double Fd(double x)
        {
            // provide default implementation
        }
        // ...
    }
    

    If you want to stick to functional programming, I would use composition instead of inheritance:

    // Does not descend from Analyzer. Could implement IAnalyzer.
    public class PolyAnalyzer {
        private readonly Analyzer analyzer;
        private double[] coefs;
    
        public PolyAnalyzer( params double[] coef ) {
            coefs = coef;
    
            analyzer = new Analyzer(poly);
        }
    
        public double evaluate(double x) {
            return analyzer.evaluate(x);
        }
    
        // Implement evaluateDerived and solve through delegation
    
        private double poly( double x ) {
            double sum = 0;
            for ( int i = 0 ; i < coefs.Length ; i++ ) {
                sum += coefs[i] * Math.Pow(x,coefs.Length-1-i);
            }
            return sum;
        }
    }
    

    Alternately, if you took @Reed Copsey’s advice and switched to Func<double, double>, you could use closures in a factory method:

    public static class PolyAnalyzerFactory {
        public static Analyzer Create( params double[] coef ) {
            var coefs = coef.ToArray();  // protect against mutations to original array
    
            return new Analyzer(
                x =>
                {
                    double sum = 0;
                    for ( int i = 0 ; i < coefs.Length ; i++ ) {
                        sum += coefs[i] * Math.Pow(x,coefs.Length-1-i);
                    }
                    return sum;
                });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code snippet below. class X { public String toString() { return
Consider the code below: <?php class Base { protected $name = Base; public function
Consider the DUPoint class, whose declaration appears below. Assume this code appears in a
A bit of help please, consider the bit of code below. public class Widget
Consider code sniper below: package sync; public class LockQuestion { private String mutable; public
Consider the sample code below: #include <iostream> using namespace std; class core { public:
I am having trouble finding an answer to this. Consider the clipping code below:
Consider the example below: #include <iostream> using namespace std; class base { public: virtual
Consider the following code in a DLL: public class ReceivingClass { private Assembly myAssembly;
Consider the below C++ code class B; class A{ private: B* mB; }; class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.