|
BillWoodruff wrote: Re Dictionary: since Keys are implemented as Hashes, does this affect search by Linq ?
Idea of a Dictionary/hash is that it first uses the hash to find a bucket then uses the bucket (list) to find an exact match. Within the constraints of the data, especially larger sets, using that algorithm can be much more efficient than just searching a list sequentially. With all things using is correctly for the case is important.
Dictionaries can also iterate on all of the contents. So you use it just like a list. And in the cases I have seen people have been doing that.
BillWoodruff wrote: The canonical way to search for a (possibly not present) Key in a Dictionary is via
The programmers just want to find it by key but use linq which instead resolves to a list lookup. And in that case it is a programmer fault not linq.
|
|
|
|
|
Hello,
Can we remove a page from a pdf document using C#? I started to look at the
PrintDocument object part of the
System.Drawing.Printing; namespace. In a bit of crunch time, please let me know if this is possible before we take this option of the table.
thanks a bunch!
|
|
|
|
|
PrintDocument doesn't do anything with or to PDF files - it has no idea they even exist!
All it is concerned with is letting you format and convert your data to a format that the selected printer understands - no more than that.
If you actually want to remove a page from a PDF file, you will have to access and modify the file itself, which you do by reading the PDF file, and only importing the pages you want - then exporting the data to a (preferably new) file.
You can do that with iTextSharp, I believe, but I've not tried it myself: c# - itextsharp trimming pdf document's pages - Stack Overflow[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you so much for clarifying. Really appreciate the timely response!
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
using System;
using System.IO;
using System.Globalization;
namespace NamespaceName
{
public class ClassName
{
const int maxiter = 1000;
const double eps = 1e-12;
public static double hypot(double a,double b)
{
double absa,absb;
absa = Math.Abs(a);
absb = Math.Abs(b);
if(absa > absb) return absa * Math.Sqrt(1+(double)(absb/absa)*(double)(absb/absa));
else return (absb == 0?0: absb * Math.Sqrt(1 + (double)(absa/absb)*(double)(absa/absb)));
}
public static void printMatrix(int m,int n, double[,] A)
{
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalDigits = 12;
for(int i = 0;i < m;i++)
{
for(int j = 0; j < n; j++)
{
Console.Write("{0} , ",A[i,j].ToString("N",nfi));
}
Console.WriteLine();
}
Console.WriteLine();
}
public static void QR_Givens(int m,int n,double [,] A,double[,] Q)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < m; j++)
Q[i,j] = (i == j ? 1 : 0);
int min = (m < n ? m : n);
for(int i = 0; i < min; i++)
{
for(int j = i + 1; j < m; j++)
{
if(A[j,i] != 0)
{
double r = hypot(A[i,i],A[j,i]);
double c = (double) (A[i,i]/r);
double s = (double) (A[j,i]/r);
for(int k = 0;k < n; k++)
{
double temp = A[i,k];
A[i,k] = c * A[i,k] + s * A[j,k];
A[j,k] = -s * temp + c * A[j,k];
}
for(int k = 0;k < m;k++)
{
double temp = Q[k,i];
Q[k,i] = c * Q[k,i] + s * Q[k,j];
Q[k,j] = -s * temp + c * Q[k,j];
}
}
}
}
}
public static void copyMatrix(double[,] A,double[,] B,int m,int n)
{
for(int i = 0;i < m;i++)
for(int j = 0;j < n;j++)
B[i,j] = A[i,j];
}
public static void multiplyMatrix(double[,] A,double[,] B,double[,] C,int m,int n,int p)
{
for(int i = 0;i < m;i++)
for(int j = 0;j < p;j++)
{
double sum = 0;
for(int k = 0;k < n;k++)
sum += A[i,k] * B[k,j];
C[i,j] = sum;
}
}
public static double rayleigh(int n, double[,] A,double[] q)
{
double norm = 0;
double sum;
double[] v = new double[n];
for(int i = 0;i < n;i++)
norm += q[i] * q[i];
for(int i = 0;i < n;i++)
{
sum = 0;
for(int j = 0;j < n;j++)
sum += A[i,j]*q[j];
v[i] = sum;
}
double r = 0;
for(int i = 0;i < n;i++)
r += q[i]*v[i];
r /= (double)norm;
return r;
}
public static void Main(string[] args)
{
char esc;
int n;
double[,] A,Q,R;
double[] v;
double[] a;
double r;
Random rnd = new Random();
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalDigits = 12;
using(StreamWriter sw = new StreamWriter("polyroots.txt",true))
{
do
{
Console.WriteLine("Podaj stopien wielomianu");
int.TryParse(Console.ReadLine(),out n);
A = new double[n,n];
Q = new double[n,n];
R = new double[n,n];
v = new double[n];
a = new double[n + 1];
for(int i = n;i >= 0;i--)
{
Console.Write("Podaj a[{0}]= ", i);
double.TryParse(Console.ReadLine(),out a[i]);
}
for(int i = n;i >= 0;i--)
if(a[i] < 0)
sw.WriteLine("-{0}x^{1} ",(-a[i]).ToString("N",nfi),i);
else
sw.WriteLine("+{0}x^{1} ",a[i].ToString("N",nfi),i);
sw.WriteLine();
for(int i=0;i<n;i++)
A[0,i] = (double)(-a[n-i-1]/a[n]);
for(int i = 1;i < n;i++)
for(int j = 0;j<n;j++)
A[i,j] = (i == j+1)?1:0;
printMatrix(n,n,A);
for(int i = 0;i < n;i++)
{
for(int j = 0; j < n; j++)
sw.Write("{0} ",A[i,j].ToString("N",nfi));
sw.WriteLine();
}
sw.WriteLine();
for(int i = 0;i < n;i++)
v[i] = i + rnd.NextDouble();
for(int i = 0;i < maxiter;i++)
{
r = rayleigh(n,A,v);
for(int j = 0;j < n;j++)
A[j,j] -= r;
QR_Givens(n,n,A,Q);
copyMatrix(A,R,n,n);
multiplyMatrix(R,Q,A,n,n,n);
for(int j = 0;j < n;j++)
A[j,j] += r;
for(int j = 0;j < n;j++)
v[j] = Q[n-1,j];
}
printMatrix(n,n,A);
for(int i = 0;i < n;i++)
{
for(int j = 0; j < n; j++)
sw.Write("{0} ",A[i,j].ToString("N",nfi));
sw.WriteLine();
}
sw.WriteLine();
Console.WriteLine("Pierwiastki danego rownania wielomianowego to: ");
sw.WriteLine("Pierwiastki danego rownania wielomianowego to: ");
int k = 0;
while(k<n)
{
if(k + 1 < n && Math.Abs(A[k+1,k])>eps)
{
double p = 0.5*(A[k,k]+A[k+1,k+1]);
double q = A[k,k] * A[k+1,k+1] - A[k,k+1] * A[k + 1,k];
double d = q - p * p;
Console.WriteLine("x[{0}]={1}-{2}i",k,p.ToString("N",nfi),Math.Sqrt(d).ToString("N",nfi));
Console.WriteLine("x[{0}]={1}+{2}i",k+1,p.ToString("N",nfi),Math.Sqrt(d).ToString("N",nfi));
sw.WriteLine("x[{0}]={1}-{2}i",k,p.ToString("N",nfi),Math.Sqrt(d).ToString("N",nfi));
sw.WriteLine("x[{0}]={1}+{2}i",k+1,p.ToString("N",nfi),Math.Sqrt(d).ToString("N",nfi));
k += 2;
}
else
{
Console.WriteLine("x[{0}]={1}",k,A[k,k].ToString("N",nfi));
sw.WriteLine("x[{0}]={1}",k,A[k,k].ToString("N",nfi));
k++;
}
}
esc = (char) Console.ReadKey().Key;
}
while(esc != (char)ConsoleKey.Escape);
}
}
}
}
How can i improve this code
Is Rayleigh quotient shift calculated correctly
Maybe other shift would be better
What other improvements could be made
|
|
|
|
|
What is wrong for you with this presented code?
Doesn't it work properly?
Does it work tooooo long?
Why should it be improved?
|
|
|
|
|
It works but it could work more efficiently
For example is Rayleigh quotient calculated properly
or maybe i could use better shift and how to do it
Maybe better stop condition ?
Did you have numerical methods ?
Have you remember topics from this
|
|
|
|
|
You can improve the code by using real variable names, not A, B C.....
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You are very wise did you know that ?
|
|
|
|
|
You asked.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Finding roots of a polynomial is an ill-conditioned problem. There are better ways of dealing with this problem than using matrices.
|
|
|
|
|
Hello Members,
I am working on project and need to communicate LPC 2132 microcontroller and PC. I have connected PC and microcontroller through RS232 & RJ 45 cables.I am doing this communication in visual studio with c#. I have some coommand which I need to write and get response from microcontroller.
For ex: I have hex command- 0x25 0x52 0x02 0x01 0xDE
Response command- 0x25 0x0C 0x01 UHD-4k 02.10.09 0xC
I have created userinterface and when I send any hex command, the I get same command in terminal. What should I do to get resonse command if I put that above command.
Please help me out coz I am working on this since many days.
Regards,
Sanjay Siwach
|
|
|
|
|
Without seeing what your code is doing, or how you are handling data, we really can't help you - remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
So show us the relevant code fragments - not the whole project, just the important bits - and tell us what you have tried and why it didn't work over the last "many days".
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Message Closed
modified 15-Feb-22 16:49pm.
|
|
|
|
|
What part ofQuote: So show us the relevant code fragments - not the whole project, just the important bits did you have difficulty understanding?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
When I send byte array command to microcontroller, the I get repsonse from microcontroller whatever I put condition in btnSend_click line.I have protocol command for this microcontrller, so, I just want to understand that I have to write condition what I want response from microcontroller or microcontroller should give me response automatically as protocol.
For ex_ I put condition here-
private void btnSend_Click(object sender, EventArgs e)
{
string s = txtSend.Text;
switch (s)
{
case "25520201DE":
rtxtDataArea.Text = "250C01UHD-4K02.10.09C2";
break;
default:
rtxtDataArea.Text = "busy";
break;
}
}
Hope you understood my point. Thankyou for your prompt reply. Really mean to me!
|
|
|
|
|
That doesn't send anything anywhere, or receive anything from anywhere. TextBoxes display to the user and fetch user input, they have nothing to do with RS232.
And your receive data event handler overwrites the existing data each time it gets a character! You do realize that serial data arrives byte by byte and that you get a DataReceived event fired for each?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello,
Thankyou for your prompt reply. I feel sorry to reply late because of some problem.
Please read the following and let me know what will be procedure to do:-
Write “Key – KEY_WB” request
« %W 0x03 0x02 0xF3 LRC »
____________________________________________
Write “Key – KEY_WB” response
« % W 0x02 ACK LRC »
LRC = XOR complete message
STAT = ACK (0x06) = OK
NAK (0x15) = not OK
BUSY (0x07) = in process
In case of negative answer (NACK) the request must be repeated until a positive answer
(ACK) has been received. The delay time for a new request has a minimum of 100ms.
In case of answer (BUSY) the sender has to wait until the answer (ACK) has been
received.
In every case; each request must be answered with an response. The host has to wait on
an response.
|
|
|
|
|
Most probably your LPC2132 is configured in 'echo mode'. That means it does return every received character. This is a kind of simple 'handshake'.
|
|
|
|
|
Hello,
Let me explain the procedure so you can understand fully.I have system which includes microcontroller LPC 2132 and the system has 6 keypad Menu, so, I need to get these menu function to PC. Every key has specific command with which the microcontroller will respond some command. I connected the system(DB-9) and serial interface PC(RJ45) and uploaded firmware through flash magic software. After uploading this, I need to write a programm having some commands according to key button on the keypad of the system. I have created GUI with all essential requirement like baud rate, parity, stop, hex command, connect and send data.
Note<:- When I connect the system and PC through user interface, automatically it show command continuously in terminal which i created on GUI that command which are following
Read = 0x90 & Read = 0x9F
Then I send some hex command in send command then it does not show anything, so, i need to press 'Standbykey' button on the system then I get same command in terminal which is following:-
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
private void btnSend_Click(object sender, EventArgs e)
{
sendData();
}
But, if I change the structure with some command in btnSend_Click(object sender, EventArgs e) then
private void btnSend_Click(object sender, EventArgs e)
{
case "25520201DE":
rtxtDataArea.Text = "25520C01UHD-4K02.10.09C2"
break;
default:
rtxtDataArea.Text = "busy"
}
I get waht I want in terminal(richtextbox). This is the result.
Please let me know any other programm i need to write or change anything. Thank you for your support.Hope you understood my point.
}
|
|
|
|
|
When a peripheral seems to return whatever you send to it, that usually means your cabling is faulty, e.g. you forgot to connect the voltage reference ("ground"), or you switched data in and data out wires.
Suggestion: get your hardware in working order before investing in software.
Things that may help:
1. write a simple (I repeat: simple) program that just sends a command periodically, at best something your peripheral should react on with some visual feedback (e.g. blinking a LED).
Best: add some LEDs to your peripheral and implement one-byte commands to turn them on/off.
("observability is the key to success")
2. alternatively: do whatever it takes to make your peripheral send known data, and capture that with a (virtual) terminal.
3. do make sure your serial port parameters (baud rate, parity, stop bits,...) are identical on both sides.
As long as neither 1 or 2 works, your hardware connection is not good.
PS: NEVER write software that swallows exceptions; they exist to tell you what goes wrong where; ignoring them (or reducing them to error=true) is a criminal offence.
Luc Pattyn [My Articles]
The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.
|
|
|
|
|
Luc Pattyn wrote: NEVER write software that swallows exceptions; So true.
I inherited a project that had this, after a good amount of time trying to trace down a problem, I eventually stumbled upon an empty catch block, rage could not adequately describe what I felt.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
modified 12-Jan-22 13:42pm.
|
|
|
|
|
I connected serial interface PC(RJ-45) and sytsem includes LPC2132 (DB-9) and uploaded firmware through flash magic software with setting of same baud rate, parity, stop bits . Then I created GUI with terminal in visual studio with c#.Net and wrote programm accordingly.Please check the following:-
//Convert a string of hex digits (example: E1 FF 1B) to a byte array.
//The string containing the hex digits (with or without spaces)
//Returns an array of bytes.
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
private void btnSend_Click(object sender, EventArgs e)
{
sendData();
}
In this way when I send commands "0x25 0x52 0x02 0x01 0xDE" , I get the same in virtual termanl.
When I put the command in btnSend_click(object sender, EventArgs e)
like
private void btnSend_Click(object sender, EventArgs e)
{
case "25520201DE":
rtxtDataArea.Text = "25520C01UHD-4k02.10.09C2";
break;
default:
rtxtDataArea = "busy";
break;
}
I get the result what I wrote in rtxtDataArea.Text.
Note:- The cable is not purchased from anywhere but prepared for this specific purpose.I think cable is not faulty.
If you can check my command things, are these are correct if not please let me know is any other way to do it. Thankyou for your support.
|
|
|
|
|
Hi,
1. your HexStringToByteArray() method seems OK as long as the input string is just hex and spaces. It does not contain error handling.
2. A wild guess: add the following to your port initialization:
ComPort.Handshake=Handshake.None;
ComPort.Encoding=Encoding.GetEncoding(28591);
Luc Pattyn [My Articles]
The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.
|
|
|
|
|