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 4029984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:24:52+00:00 2026-05-20T11:24:52+00:00

Here is the C# code I am trying to translate: public bool equals(Matrix matrix,

  • 0

Here is the C# code I am trying to translate:

public bool equals(Matrix matrix, int precision)
    {

        if (precision < 0)
        {
            throw new MatrixError("Precision can't be a negative number.");
        }

        double test = Math.Pow(10.0, precision);
        if (double.IsInfinity(test) || (test > long.MaxValue))
        {
            throw new MatrixError("Precision of " + precision
                    + " decimal places is not supported.");
        }

        precision = (int)Math.Pow(10, precision);

        for (int r = 0; r < this.Rows; r++)
        {
            for (int c = 0; c < this.Cols; c++)
            {
                if ((long)(this[r, c] * precision) != (long)(matrix[r, c] * precision))
                {
                    return false;
                }
            }
        }

        return true;
    }

Here is what I have so far:

type Matrix(sourceMatrix:double[,]) =
     let rows = sourceMatrix.GetUpperBound(0) + 1
     let cols = sourceMatrix.GetUpperBound(1) + 1
     let matrix = Array2D.zeroCreate<double> rows cols
     do
      for i in 0 .. rows - 1 do
      for j in 0 .. cols - 1 do
        matrix.[i,j] <- sourceMatrix.[i,j]
    ///The number of Rows in this Matrix.
    member this.Rows = rows

    ///The number of Columns in this Matrix.
    member this.Cols = cols

    member this.Equals(matrix:Matrix, precision:int) =
     if(precision < 0) then raise (new ArgumentOutOfRangeException("Precision can't be a negative number."))
     let (test:double) = Math.Pow(10.0, double(precision))
     if(System.Double.IsInfinity(test) || (test > double(System.Int32.MaxValue))) then raise (new ArgumentOutOfRangeException("Precision of " + precision.ToString() + " decimal places is not supported."))
     let precision = int(Math.Pow(10.0, double(precision)))

As you can see what I have written so far is loaded with type casts which probably means my code is not written the way it should be. The unfinished part needs the method to return on the first element which returns false when evaluated to a certain precision. I’m sure there must be some elegant F# code to achieve this and clearly I am nowhere near it. I was trying to figure out if the Array2D class had some method on it that would allow me to do this, but I wasn’t able to find it if there is. I am aware of the PowerPack Matrix class and will use it eventually, but for now I’m trying to learn F# by translating C# code I understand into F#. Easier said than done apparently. 🙂 I believe I’ve added all the relevant F# code in the type I’m creating. Let me know if I’m missing something.

  • 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-05-20T11:24:53+00:00Added an answer on May 20, 2026 at 11:24 am

    An elegant and high-level way to write this that probably won’t be extremely efficient is to use lazy sequence expressions:

    seq { for r in 0 .. this.Rows - 1 do
            for c in 0 .. this.Cols - 1 do
              if <your condition goes here> then
                yield false}
    |> Seq.forall id
    

    The idea is that the sequence will generate false as soon as the first element in the matrix matches the condition. Then the Seq.forall function immediately returns false (and stops iterating over the sequence).

    In practice, you’ll probably need to implement this using recursive function to make it efficient. This is not particularly nice (because breaking out of loops cannot be done in F#), but you shouldn’t need code like this too often:

    let rec loopRows r = 
      let rec loopCols c = 
        if c = this.Cols then true
        elif <your condition goes here> then false
        else loopCols (c + 1)
      if r = this.Rows then true       // Processed all rows
      elif not (loopCols 0) then false // Nonequal element in this row
      else loopRows (r + 1)            // Continue looping
    
    loopRows 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying 'translate' the sample code in here , but i can't find equivalent
Can you help me translate a Servlet to JSP here's the code: package Inventory;
I am trying to reduce some code here. I will explain how I have
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
I am trying to reuse Apple's Speak Here sample code in my own iPhone
I am trying to install MySQLdb package. I found the source code here .
I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my
I'm trying to update an element in the XML document below: Here's the code:
I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try

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.